[英]Can ng-change call another controller?
我有以下HTML
<div ng-controller="select">
<select ng-options="n for n in names"
ng-model="selected.item"
ng-change="change()">
</select>
</div>
<div ng-controller="graph">
<input type="text" ng-model="mySelected.item">
<canvas id="line" class="chart chart-line" chart-labels="gs" chart-data="ser">
</canvas>
</div>
我希望ng-option中的change()方法調用“圖形”控制器。 基本上,如果更改了選項,則應更新圖形。 有沒有辦法做到這一點。 請幫忙。
如果需要,我還將共享我的控制器代碼:
app.controller('graph', ['$scope', '$http', 'myService', function($scope,$http, myService) {
$scope.mySelected = myService.selected;
console.log($scope.mySelected);
//$http.get('/myapp/stocklist/'+ $scope.mySelected).
$http.get('/myapp/stocklist/AMZN').
success(function(data) {
gs = [];
ser = [];
for( var i=0; i<data.length; i++ ) {
gs.push(data[i].name);
ser.push(data[i].high);
}
$scope.gs=gs;
$scope.ser=[];
$scope.ser.push(ser);
});
}]);
app.controller('select', ['$scope', '$http', 'myService', function($scope,$http, myService) {
$scope.selected = myService.selected;
$http.get('/myapp/stocknames').
success(function(data) {
$scope.names=data;
console.log($scope.names);
});
}]);
在圖形控制器中,您可以使用$ on:
scope.$on('onServiceSelected', function (event, selectedService) {
$scope.mySelected = selectedService;
});
在另一個控制器中,您將使用$ broadcast:
$rootScope.$broadcast('onServiceSelected',$scope.serviceSelection);
這應該為您指明正確的方向。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.