如何以编程方式从AngularJS控制器语法的别名中获取名称?
<servlet-mapping>
<servlet-name>application_ui</servlet-name>
<url-pattern>https://*:443/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>client_inlet</servlet-name>
<url-pattern>https://*:4443/ClientInlet</url-pattern>
</servlet-mapping>
我如何获得MyController as mine
变量?
要清楚,我mine
有多种用法:
MyController
我在MyController中如何获得MyController as use1
MyController as use2
或use1
答案 0 :(得分:2)
以下是如何使用它
var app = angular.module("myShoppingList", []);
app.controller("myCtrl", function($scope) {
var mine = this; //You can change "mine" with whatever you want
mine.products = ["Milk", "Bread", "Cheese"];
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myShoppingList" ng-controller="myCtrl as mine"> <!--Use the choosen name-->
<ul>
<li ng-repeat="x in mine.products">{{x}}</li>
</ul>
</div>
&#13;
答案 1 :(得分:1)
&#34; Controller As&#34; Angular JS 1.2.0中显示的语法
angular.module('app')
.controller('Customers', [function() {
var vm = this;
vm.title = 'Customers';
vm.customers = [
{name: 'Haley'}, {name: 'Ella'}, {name: 'Landon'}, {name: 'John'}
];
}]);
在视图中使用它
<article ng-controller="Customers as vm">
<ul ng-repeat="c in vm.customers">
<li></li>
</ul>
</article>