Angular控制器不与视图对话。包含ng-app,也是ng-controller。
使用meanJS。
以下是观点:
<section ng-app="my-module" ng-controller="MyModuleController">
<div>
{{ costumers[0] }}
</div>
</section>
这是控制器:
(function() {
'use strict';
angular
.module('my-module')
.controller('MyModuleController', MyModuleController);
MyModuleController.$inject = ['$scope'];
function MyModuleController($scope) {
var vm = this;
// My module controller logic
// ...
$scope.costumers = [{
name: 'John Doe',
city: 'New York'
}, {
name: 'Peter Griffin',
city: 'San Francisco'
}];
init();
function init() {
}
}
})();
答案 0 :(得分:1)
尝试使用vm
代替$scope
:
function MyModuleController($scope) {
var vm = this;
vm.costumers = [{
name: 'John Doe',
city: 'New York'
}, {
name: 'Peter Griffin',
city: 'San Francisco'
}];
init();
function init() {
}
}
HTML:
<section ng-app="my-module" ng-controller="MyModuleController as vm">
<div>
{{ vm.costumers[0] }}
</div>
</section>
答案 1 :(得分:0)
将空依赖项注入器初始化为app模块,如下所示
angular
.module('my-module',[])
.controller('MyModuleController', MyModuleController);
小方注。如果您希望在控制器内使用this
声明变量,请避免使用scope
。在html中也使用控制器作为
(function() {
'use strict';
angular
.module('my-module',[])
.controller('MyModuleController', MyModuleController);
MyModuleController.$inject = ['$scope'];
function MyModuleController($scope) {
var vm = this;
// My module controller logic
// ...
$scope.costumers = [{
name: 'John Doe',
city: 'New York'
}, {
name: 'Peter Griffin',
city: 'San Francisco'
}];
init();
function init() {
}
}
})();
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<section ng-app="my-module" ng-controller="MyModuleController">
<div>
{{ costumers[0] }}
</div>
</section>
&#13;
答案 2 :(得分:0)
这是固定的路由文件:
(function() {
'use strict';
//Setting up route
angular
.module('my-module')
.config(routeConfig);
routeConfig.$inject = ['$stateProvider'];
function routeConfig($stateProvider) {
// My module state routing
$stateProvider
.state('my-module', {
url: '/my-module',
templateUrl: 'modules/my-module/client/views/my-module.client.view.html',
controller: 'MyModuleController',
controllerAs: 'vm'
});
}
})();