大佬教程收集整理的这篇文章主要介绍了angularjs – Angular $scope作为一个对象文字或多个$scope,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
angular.module('scopeExample',[]) .controller('MyController',['$scope',@R_772_1013@) { $scope.username = 'World'; $scope.sayHello = function () { $scope.greeTing = 'Hello ' + $scope.username + '!'; }; }]);
有什么理由我不应该使用对象文字
angular.module('scopeExample',@R_772_1013@) { $scope.viewmodel = { greeTing: '',username: 'World',sayHello: function(){ this.greeTing = 'Hello ' + this.username + '!'; } }; }]);@H_874_2@
您的控制器代码最终可能如下所示:
angular.module('scopeExample',function () { var self = this; this.greeTing = ''; this.username = ''; this.getName = function () { self.greeTing = 'Hello ' + self.username + '!'; }; }]);
在视图中使用COntroller As声明将产生以下结果:
<div data-ng-controller="UserController as user"> Hello {{ user.username }} </div>
因此,在此示例中,您最终得到的代码较少,但您在视图中保留了点符号.
请注意,在Angular 1.2.0之前,Controller As功能不可用
@H_874_2@ @H_874_2@以上是大佬教程为你收集整理的angularjs – Angular $scope作为一个对象文字或多个$scope全部内容,希望文章能够帮你解决angularjs – Angular $scope作为一个对象文字或多个$scope所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。