我想将控制器注入其他模块。我有两个文件
index.html的代码在
之下<body ng-app="mainApp" ng-controller="mainCtrl">
<h1>{{lab}}</h1>
<h1 ng-controller="secCtrl">{{lab2}}</h1>
</body>
和app.js如下
var myapi = angular.module('mainApp', []);
myapi.controller('mainCtrl', function($scope){
$scope.lab='Hello'
return{
controller:'secCtrl'
}
});
myapi.controller('secCtrl', function($scope) {
$scope.lab2='Hello you are secCtrl'
});
答案 0 :(得分:1)
需要做一些澄清:
inject a controller into other module
?
mainCtrl
内使用return语句。<强>样本强>
var myapi = angular.module('mainApp', []);
myapi.controller('mainCtrl', function($scope){
$scope.lab='Hello';
});
myapi.controller('secCtrl', function($scope) {
$scope.lab2='Hello you are secCtrl'
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="mainApp" ng-controller="mainCtrl">
<h1>{{lab}}</h1>
<h1 ng-controller="secCtrl">{{lab2}}</h1>
</div>
答案 1 :(得分:0)
我不知道services
是什么意思,但是如果你想在两个模块之间共享数据,你可以使用app.factory('myService', function() {
var message= [];
return {
set: set,
get: get
}
function set(mes) {
message.push(mes)
}
function get() {
return message;
}
});
myapi.controller('mainCtrl', function($scope,myService){
$scope.lab='Hello'
myService.set($scope.lab);
});
在ctrl1
中myapi.controller('secCtrl', function($scope,myService) {
$scope.lab2= myService.get();
});
在ctrl2
中[IO TerrianType]