我正尝试在AngularJs中的两个不同控制器之间进行通信。我想实现的目标是在第一个控制器中使用变量的值,一旦通过用户下拉选择而不是我设置的默认值。但不幸的是第二个控制器使用第二个控制器中的变量的默认值。如何在第一个控制器中调用第二个控制器后才能在AngularJs中执行的控制器中的某个函数
我是Angular的新手,所以如果你认为它没有得到很好的表现,请原谅我。我来自数据科学背景。 这里是我使用
<script>
var app = angular.module("BuildApp", []);
app.run(function ($rootScope) {
$rootScope.$on('scope.stored', function (event, data) {
console.log("scope.stored", data);
});
});
app.controller("BranchController", function ($scope, $http, Scopes) {
Scopes.store('BranchController', $scope);
$http.get('http://192.168.3.96:8082/engine-api/1.1/builds/getbranch').
success(function (data, status, headers, config) {
$scope.branchs = data;
}).
error(function (data, status, headers, config) {
console.log('Api call failed', status)
});
$scope.selectedBranch = 'Nothing Selected';
$scope.dropboxitemselectedbranch = function (item) {
$scope.selectedBranch = item;
}
});
app.controller("KitController", function ($scope, $http, Scopes) {
Scopes.store('KitController', $scope);
var url = 'http://192.168.3.96:8082/engine-api/1.1/builds/getkit?Branch=' + Scopes.get("BranchController").selectedBranch;
console.log(url)
$http.get(url).
success(function (data, status, headers, config) {
$scope.kits = data;
}).
error(function (data, status, headers, config) {
console.log('Api call failed', status)
});
$scope.selectedKit = "Nothing Selected";
$scope.dropboxitemselectedkit = function (item) {
$scope.selectedKit = item;
}
});
app.factory('Scopes', function ($rootScope) {
var mem = {};
return {
store: function (key, value) {
mem[key] = value;
},
get: function (key) {
return mem[key];
}
};
});
</script>
代码和HTML的样子以下
<body ng-app="BuildApp">
<h1>Branch</h1>
<div ng-controller="BranchController">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> {{selectedBranch}}
<span class="caret"></span></button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1" >
<li ng-repeat="branch in branchs"><branch ng-click = "dropboxitemselectedbranch(branch.name)"> {{branch.name}} </branch></li>
</ul>
</div>
</div>
<h1>Kit</h1>
<div ng-controller="KitController">
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu2" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> {{selectedKit}}
<span class="caret"></span></button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu2" >
<li ng-repeat="kit in kits"><kit ng-click = "dropboxitemselectedkit(kit.name)">{{kit.name}}</kit></li>
</ul>
</div>
</div>
</body>