我正在进行AngularJS的在线课程,他们建议写这样的控制器:
控制器:
app.controller("TestController", function() {
this.printable = "Hello, World";
});
查看:
<div ng-controller="TestController as test">
<h1>{{ test.printable }}</h1>
</div>
这很奇怪,因为我读过的其他教程都建议将控制器写为:
app.controller("TestController", ["$scope", function($scope) {
$scope.printable = "Hello, World";
});
这两种方法的区别是什么?
答案 0 :(得分:1)
不同之处在于您与之交互的对象的范围。 $scope
是注入控制器的对象。您的控制器是代表角度应用的给定部分的对象。
在Angular早期,控制器不是对象。他们现在。
你应该做得很好(如果你关注哪个是正确的),但controller as
更简洁一点。