Reputation: 820
I'm testing out Angular.js (quite enjoying it), but this might be more of a pure js question in fact.
The exercise scenario is that I have two matches:
Here I am representing the first match:
$scope.match1 = {
p1: "Player A",
p2: "Player B",
winner: "to be determined"
};
When I set up the second match, I declare a variable with a reference to the match 1 winner, as such:
$scope.match2 = {
p1: $scope.match1.winner,
p2: "Player C",
winner: "tbd"
};
Now I have a button click which assigns a winner to match 1, but THIS VALUE IS NOT PASSED to match 2 (match2.p1 value remains "to be determined" although it references match1.winner which is now updated).
What gives? Thanks in advance for any help or contribution!
Here is a fiddle to play with: http://jsfiddle.net/legolandbridge/mAjX5/2/
Upvotes: 3
Views: 62
Reputation: 25882
That is because your winner
field is a string not an object
. And you cannot get a reference to String. That will be value.
Have winner
field as an object
so when you will change your winner
's value it will be changed in match2
also.
Try something like bellow.
function AppCtrl($scope) {
$scope.match1 = { // p1 vs p2
p1: "Player A",
p2: "Player B",
winner: {name:"to be determined"} //Object
};
$scope.match2 = {
p1: $scope.match1.winner, // an reference to winner of match one
p2: "Player C",
winner: "to be determined"
};
$scope.getWinner = function() { //on click
$scope.match1.winner.name = "Player B";
console.log($scope.match2.p1); //value is updated
};
}
Upvotes: 6