简体   繁体   English

ng-change可以呼叫另一个控制器吗?

[英]Can ng-change call another controller?

I have the following html 我有以下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>

I want the change() method in ng-option to call the "graph" controller. 我希望ng-option中的change()方法调用“图形”控制器。 Basically if the option is changed the graph should be updated. 基本上,如果更改了选项,则应更新图形。 Is there a way to do it. 有没有办法做到这一点。 Please help. 请帮忙。

I am also sharing my controller code if required:` 如果需要,我还将共享我的控制器代码:

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);          
});
}]);

In your graph controller you would use $on: 在图形控制器中,您可以使用$ on:

    scope.$on('onServiceSelected', function (event, selectedService) {
           $scope.mySelected = selectedService;
    });

In your other controller you would use $broadcast: 在另一个控制器中,您将使用$ broadcast:

  $rootScope.$broadcast('onServiceSelected',$scope.serviceSelection);

This should point you in the right direction. 这应该为您指明正确的方向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM