问题
What I need is to show some values automatically when I process some data on another controller, but how can I fire my $scope.setOnController1
function on controller1
when the controller2
have ended to process my data automatically. JS FIDDLE http://jsfiddle.net/b2fCE/228/
Here is HTML
the code
<div ng-app="myApp">
<div ng-controller="myController1">
<button ng-click="setOnController1()">Force values(SECOND)</button><br/>
<ul>
<li><b>myController1</b> (values won't change)</li>
<li>{{stringValue}}</li>
<li>{{objectValue}}</li>
</ul>
</div>
<div ng-controller="myController2">
<ul>
<li><b>myController2</b> (values will change when Set Values is clicked or when 2-way binding textbox is modified)</li>
<li>{{stringValue()}}</li>
<li>{{objectValue.data}}</li>
</ul>
<input type="text" ng-model="newValue"></input>
<button ng-click="setString(newValue)">Set Values(FIRST)</button><br/>
<input type="text" ng-model="objectValue.data"></input>2-way binding to objectValue
</div>
</div>
And JS
var app = angular.module('myApp', []);
app.service('sharedProperties', function() {
var stringValue = 'test string value';
var objectValue = {
data: 'test object value'
};
return {
getString: function() {
return stringValue;
},
setString: function(value) {
stringValue = value;
},
getObject: function() {
return objectValue;
}
}
});
app.controller('myController1', function($scope, sharedProperties) {
$scope.setOnController1 = function(sharedPoperties){
$scope.stringValue = sharedProperties.getString();
$scope.objectValue = sharedProperties.getObject().data;
}
});
app.controller('myController2', function($scope, sharedProperties) {
$scope.stringValue = sharedProperties.getString;
$scope.objectValue = sharedProperties.getObject();
$scope.setString = function(newValue) {
$scope.objectValue.data = newValue;
sharedProperties.setString(newValue);
//some code to set values on screen at controller1
};
});
Here is the JS FIDDLE
http://jsfiddle.net/b2fCE/228/
回答1:
There are a couple different ways. One, you can broadcast an event on Controller2 and listen for the event on Controller1 (like tymeJV suggests). You need to broadcast and listen on the $rootScope.
app.controller('myController2', function($rootScope, $scope, sharedProperties) {
$scope.stringValue = sharedProperties.getString;
$scope.objectValue = sharedProperties.getObject();
$scope.setString = function(newValue) {
$scope.objectValue.data = newValue;
sharedProperties.setString(newValue);
//some code to set values on screen at controller1
$rootScope.$broadcast("myEvent");
};
app.controller('myController1', function($rootScope, $scope, sharedProperties) {
$scope.setOnController1 = function(sharedPoperties){
$scope.stringValue = sharedProperties.getString();
$scope.objectValue = sharedProperties.getObject().data;
}
$rootScope.$on("myEvent", function() {
$scope.setOnController1(sharedProperties);
});
});
Fiddle demo
Similarly, you can setup a $watch on Controller1 to watch the value in your service.
app.controller('myController1', function($rootScope, $scope, sharedProperties) {
$scope.setOnController1 = function(sharedPoperties){
$scope.stringValue = sharedProperties.getString();
$scope.objectValue = sharedProperties.getObject().data;
}
$scope.$watch(function(){return sharedProperties.getString()}, function(newValue, oldValue){
$scope.setOnController1(sharedProperties);
});
});
Fiddle demo
回答2:
Use $broadcast
and $on
Controller 1:
$scope.$broadcast("myEvent");
Controller 2:
$scope.$on("myEvent", function() {
console.log("Lets go!");
});
If you need to share data in between, use a shared service.
来源:https://stackoverflow.com/questions/23937963/how-to-fire-a-controller-method-from-another-controller-or-directive