2016/05/24

Angular.js custom filters inject $scope

要使用$scope裡面參數則可以透過filter後面加上:$scope.參數名稱即可

<!DOCTYPE html>
<html ng-app="test">
 <head>
  <title>hi</title>
  <meta charset="utf-8">
  <script  src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script>
  <script type="text/javascript">
   var app = angular.module('test', []);
   app.controller('testController', function($scope){
    $scope.title = "是個好人";
   });

   app.filter('addTitle', function(){
    return function(name, title){
     return name + title;
    }
   });
  </script>
 </head>
 <body ng-controller="testController">
  <input type="text" ng-model="name" ng-init="name='王小明'">
  <p ng-bind="name|addTitle:title"></p>
 </body>
</html>

執行結果:

沒有留言:

張貼留言