有什么可以用QUOT的原因及; controllerAs"属性? [英] What can be the reason to use "controllerAs" property?
问题描述
我GOOGLE了,但我没有找到合适的回答我的问题,我是新来的,我对我的angularJS教程工作angularJS。
I googled but I didn't find the appropriate answer to my question, I am new to angularJS I am working on my angularJS tutorial.
我有这个angularJS行:
I have this angularJS rows:
.state('index',{
url:"/",
templateUrl:"views/index.html",
controller:"IndexCtrl",
controllerAs:"index"
})
有什么可以用的原因 controllerAs 属性?
推荐答案
几件事情:
1。减少使用范围
而不是在控制器的范围每一个数据加载,你可以简单地使用这个
加载了你需要的一切。
Instead of loading every data in the scope of a controller, you could simply use this
to load up everything that you require.
例如:
路线
state('index',{
url:"/",
templateUrl:"views/index.html",
controller:"ListCtrl",
controllerAs:"list"
})
在控制器
angular.module('feed').controller('ListCtrl', function($scope, reddit){
var vm = this;
vm.names = ["Michael", "Roy"];
});
在模板:
<ul>
<li ng-repeat="name in list.names">
<div>{{name}}</div>
</li>
</ul>
2。正确的范围使用
在多个控制器开始发挥作用,范围成为一个棘手的事情。使用 controllerAs
方法将走很长的路是解决这个。一个例子如下所示:
When multiple controllers come into play, scope becomes a tricky thing. Using controllerAs
method will go a long way is resolving this. An example is shown below:
错误:
<span>Outside Controller: Your name is: {{username}}</span>
<div ng-controller="SignupController">
<span>Inside Controller: Your name is: {{username}}</span>
<fieldset legend="User details">
<input ng-model="username">
</fieldset>
</div>
正确:
<span>Outside Controller: Your name is: {{user.name}}</span>
<div ng-controller="SignupController">
<span>Inside Controller: Your name is: {{user.name}}</span>
<fieldset legend="User details">
<input ng-model="user.name">
</fieldset>
</div>
发现,这使得全部的寄托都更加清晰图片:
礼貌:<一href=\"http://stackoverflow.com/questions/21287794/angularjs-controller-as-syntax-clarification\">AngularJs &QUOT;控制器&QUOT;语法 - 澄清
这篇关于有什么可以用QUOT的原因及; controllerAs&QUOT;属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!