我正在使用ui-router进行路由,并且将使用嵌套作用域,因此希望使用“ controller as”语法。但是,我无法计算出正确的语法/组合来访问视图中的控制器对象属性。
app.js (sets up routing)
(function() {
angular
.module('ICP_App', ['ui.router', 'satellizer', 'ngMaterial', 'ngMessages', 'xeditable'])
.config(function($stateProvider, $urlRouterProvider, $authProvider) {
$urlRouterProvider.otherwise('dashboard');
$stateProvider
.state('clients', {
url: '/clients',
templateUrl: '../partials/clients-index.html',
controller: 'ClientsController as ClientsCtrl'
})
// more routes here...
})();
ClientsController.js
(function() {
angular.module('ICP_App')
.controller('ClientsController', function($http) {
$http.get('http://api.icp.sic.com/clients')
.success(function(clients) {
var vm = this;
vm.clients = clients.data;
console.log(vm.clients);
})
.error(function(error) {
// handle here
})
});
})();
index.html
<body ng-app="ICP_App" ng-cloak>
<!-- sidebar, header etc -->
<div ui-view></div> <!-- pull in view -->
</body>
最后,clients-index.html
部分
<div class="content">
<div class="pane" ng-repeat="client in clients">
{{ client.name }}
</div>
</div>
我也试过了client in vm.clients
,无济于事。
我的控制器语法是否有问题?正如我在ui路由器代码中一样使用控制器,但是在创建控制器时不再使用。如果我再次在控制器中使用控制器,则会出现错误(Argument ClientsController is not a
)。
我应该指出,控制台日志vm.clients确实为我提供了控制台中的数据,但我似乎无法访问它。
提前致谢。
如下修改您的ClientsController
(function() {
angular.module('ICP_App')
.controller('ClientsController', function($http) {
var vm=this;
$http.get('http://api.icp.sic.com/clients')
.success(function(clients) {
vm.clients = clients.data;
console.log(vm.clients);
})
.error(function(error) {
// handle here
})
}); })();
如下修改client-index.html
<div class="content">
<div class="pane" ng-repeat="client in ClientsCtrl.clients">
{{ client.name }}
</div>
下面的链接将帮助您更深入地了解控制器的语法
https://toddmotto.com/digging-into-angulars-controller-as-syntax/
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句