Angularjs 是否有方法重命名视图中的角度变量?
在我的角度项目中,我使用了一个特定的值,在我的控制器中称为:Angularjs 是否有方法重命名视图中的角度变量?,angularjs,variables,scope,Angularjs,Variables,Scope,在我的角度项目中,我使用了一个特定的值,在我的控制器中称为: $scope.feature1.items.thisItem 在我看来,有一个特定的多次使用thisItem,将其称为{{feature1.items.thisItem}}是相当混乱的,例如: <div id="{{feature1.items.thisItem}}header> <h1> You've reached the section about {{feature1.items.thisIt
$scope.feature1.items.thisItem
在我看来,有一个特定的
多次使用thisItem
,将其称为{{feature1.items.thisItem}}
是相当混乱的,例如:
<div id="{{feature1.items.thisItem}}header>
<h1> You've reached the section about {{feature1.items.thisItem}} </h1>
</div>
是的,您可以在DOM元素的顶部使用
<div ng-app='app'>
<div ng-controller="MyController" ng-init="myVar=feature1.items.thisItem">
{{myVar}}
</div>
</div>
{{myVar}}
是!ng init正是为了这个目的而设计的—为另一个变量添加别名:
<div ng-init="thisItem = feature1.items.thisItem">
<h1> You've reached the section about {{thisItem}} </h1>
</div>
您已经到达了关于{{thisItem}}的部分
我建议不要使用ng init,它不是为此而设计的。发件人:
ngInit的唯一适当用途是对ngRepeat的特殊属性进行别名处理,如下面的演示所示。除此之外,应该使用控制器而不是ngInit来初始化作用域上的值
您需要为此创建一个自定义控制器。如下所示:
module.controller('RenameController', function($scope) {
$scope.one = null;
$scope.$watch('feature1.items.thisItem', function(value) {
$scope.one = value;
});
});
遗憾的是,ng init似乎在Angular 2中被删除了+