gpt4 book ai didi

javascript - 作为 angularjs 中 Controller 使用的引用

转载 作者:行者123 更新时间:2023-12-02 14:15:09 24 4
gpt4 key购买 nike

<h1 ng-controller="mainController as mainCtrl">{{ms}}</h1>

.controller("mainController",function(){
this.ms = "hi";
});

为什么人们在 AngularJS 中使用 as ?我通常做的只是注入(inject) $scope 并执行 $scope.ms = 'something'

最佳答案

如果这样做,则不需要注入(inject) $scope

<h1 ng-controller="mainController as mainCtrl">{{mainCtrl.ms}}</h1>

app.controller("mainController", function() {
this.ms = "hi";
});

更好地解释here .

编辑:AngularJS 官方文档中也提到了这一点。请参阅ngController .

关于javascript - 作为 angularjs 中 Controller 使用的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39103895/

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