2014년 6월 9일 월요일

Math 함수 사용하기 && Filter 만들기

AngularJS에서 Math 함수 사용하기


AngularJs scope안에서 Math 함수를 사용시 작동을 하지 않습니다.
  • 작동하는 방법
    //controller에 선언
    $scope.Math = window.Math
    //template에서 사용
    {{Math.ceil(11/10)}}
    

AngularJS Filter 를 만들어서 for 문을 만들어 보겠습니다.


  • AngularJS 에서는 ng-repeat 를 통해 for in 을 제공합니다.
  • for 문을 사용하고 싶을때는 어떻게 할까요??
    module에 filter를 사용해서 적용 가능합니다.
    //선언하는 방법
    angular.module('testApp',[]).
     filter('range', function(){
        return function(input,total){
           total = parseInt(total);
           for(var i =0; i< total; i++){
             input.push(i); 
          }
          return input;
       }
    });
    // 사용하는 방법
    
  • "page_item"
ng-repeat="i in [] | range:(12)"> {{i}} // {{$index}} 를 사용해도 됩니다. </li>

댓글 없음:

댓글 쓰기