I have to pass a bunch of inputs and outputs between two Angular controllers and I've come up with a way to do it using $broadcast. I'm using an "if" statement to make sure the inputs match the outputs, but there must be an easier way.
app.controller('Ctrl2', ['$scope', '$rootScope', function($scope, $rootScope) {
var myArray = [$scope.input1, $scope.input2];
$scope.textChange = function(whichChange) {
if (whichChange == 1) {
$rootScope.$broadcast('textChanged', $scope.input1, 1);
} else if (whichChange == 2) {
$rootScope.$broadcast('textChanged', $scope.input2, 2);
} //else if, else if, else if, etc...
};
}]);
But seems there should be a way I can replace that if statement with a single line, something like:
$rootScope.$broadcast('textChanged', $scope.relevantInput, relevantOutput);
Same question applies to the case statement in controller Ctrl1, but I figure answering the question for one controller would answer it for the other.
Also: while comments on whether this is the best way to do this in Angular are certainly welcome my question is more about avoiding if/switch/eval.