2016년 1월 15일 금요일

[AngularJS] 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
    <html >
        <head>            
            <meta charset="utf8">
            <title>angularjs test</title>
        </head>
        <body ng-app='fruitApp'>
            <div ng-controller='nameController'>
                <!-- filter 전용 함수 사용-->
                <h1>{{title|titleCase}}</h1>
                <!-- 달러화 -->
                <h1>{{100|currency}}</h1>
                <!-- 반올림 후 달러화 -->
                <h1>{{12.9|number:0|currency}}</h1>
            </div>
            <script src='http://ajax.googleapis.com/ajax/libs/angularjs/1.4.2/angular.min.js'></script>
            <script type='text/javascript'>
                var app = angular.module('fruitApp',[]);
                app.filter('titleCase',function(){
                    var titleCaseFilter = function(input){
                        //' ' 공백으로 잘라서 배열에 넣고,
                        var words = input.split(' ');
                        for(var i=0; i<words.length;i++){
                            //0번째를 대문자로 하고, 1번째부터 잘라내서 붙인다.
                            words[i] = words[i].charAt(0).toUpperCase() + words[i].slice(1);
                        }
                        return words.join(' ');
                    }
                    return titleCaseFilter;
                });
                function __nameController($scope){
                    $scope.title = 'hi my name is jteve';
                }                
                app.controller('nameController',__nameController);    
           </script>
        </body>
    </html>
cs

댓글 없음:

댓글 쓰기