动漫引擎

导航

好习惯: 用controller as 语法和$inject数组注入

angular好习惯1: 用controller as 语法和$inject数组注入

1) 像普通的JS类一样实现controller,摆脱$scope

2) 用.$inject数组注入相关模块,便于minify


在HTML中:

<div ng-controller="NetController as sc">
  <button class="button-positive" ng-click="sc.func1()">prototype function</button>
</div>


在JS中:

function NetController($scope, $http) {

}

NetController.prototype.func1 = function() {

  alert("prototype, OOOOOOOOK!");

};

//少用静态函数,因为在html中不能调用它 

NetController.func2 = function() {

  alert("static, OOOOOOOOK!");

};

 

NetController.$inject = ['$scope', '$http'];

angular.module('starter')

  .controller('NetController', NetController);


//////////////////////////////
适应NG2的变化:

* 没有 $scope

* 不用controller,强调 component

 

http://paislee.io/migrating-to-angularjs-2-0/

posted on 2017-03-09 03:17  动漫引擎  阅读(347)  评论(0编辑  收藏  举报