【发布时间】:2013-05-13 06:04:42
【问题描述】:
有两种模式用于访问控制器功能:this 和 $scope。
我应该使用哪个以及何时使用?我知道this 设置为控制器,$scope 是视图范围链中的一个对象。但是使用新的“Controller as Var”语法,您可以轻松地使用其中任何一种。那么我要问的是什么是最好的,未来的方向是什么?
例子:
-
使用
thisfunction UserCtrl() { this.bye = function() { alert('....'); }; }<body ng-controller='UserCtrl as uCtrl'> <button ng-click='uCtrl.bye()'>bye</button> -
使用
$scopefunction UserCtrl($scope) { $scope.bye = function () { alert('....'); }; }<body ng-controller='UserCtrl'> <button ng-click='bye()'>bye</button>
我个人认为this.name 与其他 Javascript OO 模式相比更直观、更自然。
请指教?
【问题讨论】:
-
使用 'this' 似乎是 Google I/O 2013 的新功能m.youtube.com/watch?v=HCR7i5F5L8c 另外,请查看以下答案:stackoverflow.com/questions/11605917/…
-
“UserCtrl as uCtrl”语法是新的吗?我没有看到它记录在 1.0.6 或 1.1.4 ngController 页面上。
-
好的,它记录在new 1.1.5 ngController page。
-
$scope 和这个codetunnel.io/angularjs-controller-as-or-scope的最佳解释
标签: angularjs