在 eggheadio 第22 课中,我看到一个控制器自行返回。为什么一定要退货?我认为只需将范围的属性分配给控制器本身的指示名称就可以了。
var app = angular.module("phoneApp",[]);
app.controller("AppCtrl", function($scope){
this.sayHi = function(){ alert("hi");}
$scope.AppCtrl = this;
//return $scope.AppCtrl = this; //why this one when above line also works
})
并在 html
<body ng-app="phoneApp">
<div ng-controller="AppCtrl">
<button ng-click="AppCtrl.sayHi()"></button>
</div>
</body>