码迷,mamicode.com
首页 > Web开发 > 详细

[AngularJS] Exploring the Angular 1.5 .component() method

时间:2015-12-21 07:03:46      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

Angualr 1.4:

.directive(‘counter‘, function counter() {
  return {
    scope: {},
   restrict: ‘EA‘,
transclude: true, bindToController: { count:
‘=‘ }, controller: function () { function increment() { this.count++; } function decrement() { this.count--; } this.increment = increment; this.decrement = decrement; }, controllerAs: ‘counter‘, template: [ ‘<div class="todo">‘, ‘<input type="text" ng-model="counter.count">‘, ‘<button type="button" ng-click="counter.decrement();">-</button>‘, ‘<button type="button" ng-click="counter.increment();">+</button>‘, ‘</div>‘ ].join(‘‘) }; });

 

Angualr1.5:

.compoment(‘counter‘,  {
    bindings: {
      count: ‘=‘
    },
    controller: function () {
      function increment() {
        this.count++;
      }
      function decrement() {
        this.count--;
      }
      this.increment = increment;
      this.decrement = decrement;
    },
    controllerAs: ‘vm‘,
    template: function($element, $attrs){
      return [
      ‘<div class="todo">‘,
        ‘<input type="text" ng-model="vm.count">‘,
        ‘<button type="button" ng-click="vm.decrement();">-</button>‘,
        ‘<button type="button" ng-click="vm.increment();">+</button>‘,
      ‘</div>‘
    ].join(‘‘);
    },
    // restrict: ‘E‘,
    // transclude: true
});

 

  • Direcitve need pass in function, compoment need pass in object.
  • ‘scope‘ and ‘bindToController‘ can be replaced with just ‘bindings‘
  • by default restrict: ‘E‘
  • by default transclude: true
  • by default, if not given controllerAs, angular will create for you and name is the same as compoment name

[AngularJS] Exploring the Angular 1.5 .component() method

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/5062365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!