레이블이 AngularJS인 게시물을 표시합니다. 모든 게시물 표시
레이블이 AngularJS인 게시물을 표시합니다. 모든 게시물 표시

2016년 1월 28일 목요일

[AngularJS] Provider service


  • 프로바이더 서비스
  • get 을 이용하는 것 외에는 factory 서비스와 유사하다
  • config을 사용해서, 모듈별로 다른 설정을 해서 사용할 수 있어서 확장이 유연하다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html >
  <head>
    <meta charset="utf-8">
  </head>
  <body ng-app='helloApp'>
    <div ng-controller='InputController' ng-init="inputNum =0">
    <span>Input</span>
    <input type='text' ng-model='inputNum' ng-click='calcNum()'>
    <p> {{getResult()}}</p>    
    </div>
    
    <div ng-controller='OutputController'>
    <span>Output</span>
    <p> {{getCurrent()}}</p>    
    </div>
        
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.12/angular.min.js"></script>
    <script>
    var hello  = angular.module('helloApp',[]);
    //Get 으로 factory 등과 동일하게 작성된다.
    hello.provider('HelloProvider',function() {
      var configValue = 0;
      this.$get = function(){
       return {
          sum: configValue,            
          add:function(num){ this.sum += num;},          
          getSum :function(){return this.sum;}
        };
      };
      this.setStartValue = function(numValue){
        console.log(numValue);
        configValue= numValue;
      };    
    });
    // 다른 점은 각 모듈의 config를 원하는 속성으로 조절해서 사용할 수 있다.
    hello.config(function(HelloProviderProvider) {
      HelloProviderProvider.setStartValue(100);
    });
        
    hello.controller('InputController'function($scope, HelloProvider){
      $scope.getResult = function(){
        return HelloProvider.getSum();
      };
      //마우스 클릭하면, 숫자가 업데이트 된다.
      $scope.calcNum= function(){
        HelloProvider.add(Number($scope.inputNum));
      };      
    });
    
    // input/output 컨트롤러는 하나의 서비스 데이터를 공유한다.
    hello.controller('OutputController'function($scope, HelloProvider){
      $scope.getCurrent = function(){
        return HelloProvider.getSum();
      };    
    });    
    </script>
  </body>
</html>
cs

2016년 1월 27일 수요일

[AngularJS] Service, controller


1. 서비스의 Data는 아래와 같이 2개의 컨트롤러에 의해 공유될 수 있다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html >
  <head>
    <meta charset="utf-8">
  </head>
  <body ng-app='helloApp'>
    <div ng-controller='InputController' 
    ng-init="inputNum =0" class="well well-lg">
    <span class="label label-default">Input</span>
    <input type='text' ng-model='inputNum' ng-change='calcNum()'>
    <p> {{getResult()}}</p>    
    </div>
    
    <div ng-controller='OutputController' class="well well-lg">
    <span class="label label-info">Output</span>
    <p> {{getCurrent()}}</p>    
    </div>
    
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js"></script>
    <script>
    var hello  = angular.module('helloApp',[]); 
    hello.factory("HelloFactory",function(){
      return {
        sum: 0,
        add:function(num){
          this.sum += num;
        },
        getSum:function(){
          return this.sum;
        }
      };
    });
    
    hello.controller('InputController'function($scope, HelloFactory){
      $scope.getResult = function(){
        return HelloFactory.getSum();
      };
      $scope.calcNum= function(){
        HelloFactory.add(Number($scope.inputNum));
      };
    });
    hello.controller('OutputController'function($scope, HelloFactory){
      $scope.getCurrent = function(){
        return HelloFactory.getSum();
      };
    });
    </script>
  </body>
</html>
cs

2. 서비스의 Data를 공유하지 않으려면, 서비스가 함수를 반환하게끔 하고,
사용하는 곳에서는 new로 생성하면 된다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
    hello.factory("HelloFactory",function(){
      return function(startValue){
        this.sum =  startValue;
        this.add = function(num){
          this.sum += num;
        };
        this.getSum = function(){
          return this.sum;
        };
      };
    });
    
    hello.controller('InputController'function($scope, HelloFactory){
      var helloService = new HelloFactory(10);
      $scope.getResult = function(){
        return helloService.getSum();
      };     
      $scope.calcNum= function(){
        helloService.add(Number($scope.inputNum));
      };      
    });
cs

[AngularJS] filter using javascript


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<!doctype html>
<html lng='utf-8'>
  <head>
    <meta charset="utf-8">
 
  </head>
  <body ng-app="memberApp">
    <div ng-controller="ListCtrl" class="panel panel-primary">
    <div class="panel-heading">User List </div>
        <h1>search한 내용들을 이용하여, paging</h1>
    <div class="panel-body" >
 
        <p>search :</p>
    <select ng-model='type' ng-change="changeSearch(type)">
      <option vlaue="id" >id</option>
      <option vlaue="name">name</option>
      <option vlaue="years">years</option>
    </select>
    <input type='text' ng-model='search[type]'/>
    <table class=table>
      <tr ng-repeat="member in members|paging:page:search">
        <td> {{member.id}} </th>
        <td> {{member.name}} </th>
        <td> {{member.tel}} </th>
      </tr>
    </table>
        <p>{{page}} page</p>
        Page Number <input type='text' ng-model='page' >
    </div>
    </div>  
    </div>
 
 
    <script src="http://code.angularjs.org/1.2.12/angular.min.js"></script>
    <script>
    var memberApp = angular.module("memberApp", []);
    memberApp.filter('paging'function($filter){
        //Get Filter Object
        //Filter using Javascript
      var filterObj = $filter("filter");
      return function(array, page, search) {
          //1. Array를 search 로 filtering 하고
        var resultArray = filterObj(array, search);
        var pageNum = page?(page -1* 100;
          //2. paging 한다.
        return resultArray.slice(pageNum, pageNum + 10);
      }
    });
    
    memberApp.controller("ListCtrl"function($scope){
        $scope.page =0;
      $scope.search = {"id":"""name":"""tel":"", $:""};
      $scope.members = [];
      for(var i = 0; i < 121; i++){
        $scope.members.push({"id":"id"+(i + 1),"name":"user"+(i+1), "tel":"123-00"+i});
      }
    });      
    </script>
  </body>
</html>
 
 
 
cs

[AngularJS] Custom filter - Paging



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
<!doctype html>
<html lng='utf-8'>
  <head>
    <meta charset="utf-8">
    <!--
      <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
    -->
  </head>
  <body ng-app="memberApp">
    <div ng-controller="ListCtrl" class="panel panel-primary">
    <div class="panel-heading">회원 목록 </div>  
    <div class="panel-body" >
    페이지 번호 <input type='text' ng-model='page' >
    <table class=table>
      <tr>
        <th ng-click='criteria ="id"'> id </th>
        <th ng-click='criteria ="name"'> name </th>
        <th ng-click='criteria ="tel"'> tel </th>
      </tr>
      <tr ng-repeat="member in members|paging:page">
        <td> {{member.id}} </th>
        <td> {{member.name}} </th>
        <td> {{member.tel}} </th>
      </tr>
    </table>
    </div>
    </div>  
    </div>
 
    <script src="http://code.angularjs.org/1.2.12/angular.min.js"></script>
    <script>
    var memberApp = angular.module("memberApp", []);
    memberApp.filter('paging'function(){
      return function(array, page) {
        var pageNum = page?(page -1* 100
        return array.slice(pageNum, pageNum + 10);
      }
    });
 
    memberApp.controller("ListCtrl"function($scope){
      $scope.members = [];
      for(var i = 0; i < 121; i++){
        $scope.members.push({"id":"user"+(i + 1),"name":"user"+(i+1), "tel":"123-00"+i});
      }
    });      
    </script>
  </body>
</html>
cs

2016년 1월 26일 화요일

[AngularJS] Drop down Filter


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!doctype html>
<html ng-app lng='utf-8'>
<head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
</head>
<body>
<div ng-controller="Ctrl" class="panel panel-primary">
    <div class="panel-heading">Dropdown Filter </div>
    <div class="panel-body">
 
        <select ng-model='type' ng-change="changeSearch(type)">
            <option vlaue="id" >id</option>
            <option vlaue="name">name</option>
            <option vlaue="years">years</option>
        </select>
 
        <input type='text' ng-model='search[type]'/>
 
        <p>{{search}}</p>
 
        <table class=table>
            <tr>
                <th ng-click='criteria ="id"'> id </th>
                <th ng-click='criteria ="name"'> name </th>
                <th ng-click='criteria ="years"'> year </th>
            </tr>
 
            <tr ng-repeat='member in members|filter:search|orderBy:criteria' >
                <td> {{member.id}} </td>
                <td > {{member.name}} </td>
                <td> {{member.years}} </td>
            </tr>
        </table>
 
    </div>
</div>
</div>
<script src="http://code.angularjs.org/1.2.12/angular.min.js"></script>
<script>
 
    function Ctrl($scope){
        $scope.members = [
            {"id":"user00""name":"AAB""years""52"},
            {"id":"user01""name":"BB1""years""46"},
            {"id":"user02""name":"BB2""years""1"},
            {"id":"user03""name":"CCC""years""2"},
            {"id":"user04""name":"CCD""years""2"},
            {"id":"user05""name":"FFF""years""32"},
            {"id":"user06""name":"GGG""years""2"}
        ];
 
        $scope.criteria = $scope.criteria|"id";
 
        $scope.search = {"id":"""name":"""years":"", $:""};
 
 
        $scope.changeSearch = function(type) {
 
            forvar prop in $scope.search){
 
                console.log(prop);
 
                if(prop !== type){
                    $scope.search[prop] = "";
                }
            }
 
        };
    }
</script>
</body>
</html>
cs

[AngularjS] Multi Filter



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<!doctype html>
<html ng-app lng='utf-8'>
<head>
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
</head>
<body>
<div ng-controller="Ctrl">
    <!-- filter Date -->
    <h2>filter Date example </h2>
    <p> {{regDate | date:'yyyy-MM-dd'}}</p>
    <p> {{regDate | date:'yyyy-MM-dd HH:mm:ss'}}</p>
    <p> medium {{regDate | date:'medium'}}</p>
    <p> short {{regDate | date:'short'}}</p>
    <p> {{regDate | date:'y'}}</p>
    <!-- filter number -->
    <h2>filter number example </h2>
    <p> {{point|number}} </p>
    <p> {{point|number: 0}} </p>
    <p> {{point|number: 2}} </p>
    <!-- orderBy:["years","-name"]' -->
    <h2>orderBy_Filter example </h2>
    Filter : <input type='text' ng-model='keyword'>
    <table class=table>
        <tr>
            <th ng-click='criteria ="id"'> id </th>
            <th ng-click='criteria ="name"'> name </th>
            <th ng-click='criteria ="years"'> year </th>
        </tr>
        <!-- <tr ng-repeat='member in members|orderBy:["years","-name"]' >-->
        <tr ng-repeat='member in members | orderBy:criteria | filter:keyword:check' >
            <td> {{member.id}} </td>
            <td > {{member.name}} </td>
            <td> {{member.years}} </td>
        </tr>
    </table>
    <h2>Multi filter using Object </h2>
    <!-- filter의 경우 $를 사용하면 객체형태로 사용되어 유용하다-->
    id : <input type='text' ng-model='keyword.id'>
    name : <input type='text' ng-model='keyword.name'>
    year : <input type='text' ng-model='keyword.years'>
    <table class=table>
        <tr>
            <th ng-click='criteria ="id"'> id </th>
            <th ng-click='criteria ="name"'> name </th>
            <th ng-click='criteria ="years"'> year </th>
        </tr>
        <!-- <tr ng-repeat='member in members|orderBy:["years","-name"]' >-->
        <tr ng-repeat='member in members | orderBy:criteria | filter:keyword:check' >
            <td> {{member.id}} </td>
            <td > {{member.name}} </td>
            <td> {{member.years}} </td>
        </tr>
    </table>
</div>
<script src="http://code.angularjs.org/1.2.12/angular.min.js"></script>
<script>
    function Ctrl($scope){
        $scope.regDate = 1288323623006;
        $scope.point =  1234.56789;
        $scope.members = [
            {"id":"user00""name":"AAB""years""52"},
            {"id":"user01""name":"BB1""years""46"},
            {"id":"user02""name":"BB2""years""1"},
            {"id":"user03""name":"CCC""years""2"},
            {"id":"user04""name":"CCD""years""2"},
            {"id":"user05""name":"FFF""years""32"},
            {"id":"user06""name":"GGG""years""2"}
        ];
        $scope.criteria= 0;
        $scope.check = function(actual, expected){
            return actual.toLowerCase().indexOf(expected.toLowerCase()) >= 0 ? truefalse;
        }
    }
</script>
</body>
</html>
cs