2017-5-21 AngularJS学习笔记

2017-05-21  本文已影响0人  GodlinE

指令作用域

<body ng-app='app' ng-controller='xmgController'>
          <input type='text' ng-model='msg'>
          <sk-dir></sk-dir>
</body>
<script src='js/angular.js'></script>
<script>
          var app = angular.module('app',[]);
          app.controller('xmgController',['$scope',function($scope){
                  $scope.msg = '666';
          }]);
          app.directive('skDir',function(){
                  return {
                          restrict:'EA',
                          template:'<h1>{{msg}}</h1>',
                          replace:true,
                          controller:function($scope){
                                  $scope.msg="我是指令的 msg"
                          }
                  }
})
</script>

过滤器

在 AngularJS 中,使用过滤器格式化展示数据,作用就是接收一个输入,通过某个规则进行处理,然后范湖处理结果

angular 内置了9个过滤器

<p>{{score | orderBy:'' : false}}</p>
<p>{{stu | orderBy:'age' :true}}</p>
............................
$scope.course = ['html','css','js'];
$scope.stu = [
          {name:'sk',age:22},                      
          {name:'sk1',age:25},
          {name:'sk2',age:12},
          {name:'sk3',age:34},
          {name:'sk4',age:23}
];
<input type="text" ng-model="search">
<p>{{curse | filter:search}}</p>
//输入一个字符,就会把包含此字符的数组元素显示出来
<ul>
        <li ng-repeat='value in course | filter:search'>{{value}}</li>
</ul>
............................
$scope.course = ['html','css','js'];
$scope.stu = [
          {name:'sk',age:22},                      
          {name:'sk1',age:25},
          {name:'sk2',age:12},
          {name:'sk3',age:34},
          {name:'sk4',age:23}
];

管道符

<p>{{price | currency:"$"}}</p>
function currency(input,arg){
        return arg + input;
}

自定义过滤器

app.fliter('skCur',function(){
        return function(input,args){
                  return args + input
        }
})
app.filter('firstUppercase',function(){
        return function(input,args){
                return input[0].toUpperCase() + input.slice(1);
        }
})

依赖注入

上一篇下一篇

猜你喜欢

热点阅读