gpt4 book ai didi

javascript - $scope 与 Angular 上的 this

转载 作者:行者123 更新时间:2023-11-28 00:42:06 24 4
gpt4 key购买 nike

哪个更好?为什么?

(function(){
var app = angular.module('myApp', []);

app.controller('MyController', function() {
this.guy = obj1;
});

app.controller('AnotherController', function ($scope){
$scope.guy = obj2;
});

var obj1 = {
'name' : 'david',
'title' : 'dude from obj1',
'company' : 'AA',
'doesIt' : 'this uses this'
}, obj2 = {
'name' : 'warren',
'title' : 'dude from obj2',
'company' : 'AA',
'doesIt' : 'this uses scope'
};
})();

我看过同时使用这两种方法的教程。这是一个偏好的事情吗?是否只是是否能够在 html attr 中使用 Controller 别名? $scope 有什么了不起的?我正在寻找一个直接的答案。谢谢。

最佳答案

controller as 语法的主要优点 - 它使 html 更加简洁:

<div ng-contoller="parentController">
<div ng-contoller="childController">
<!-- you can't say exactly, where test located-->
{{test}}
</div>
</div>

<div ng-contoller="parentController as parent">
<div ng-contoller="childController as child">
<!--it's clear where test-->
{{parent.test}}
</div>
</div>

您还可以看到this

关于javascript - $scope 与 Angular 上的 this,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27813303/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com