col*_*eau 3 javascript angularjs
我正在测试Angular.js(非常喜欢它),但事实上这可能更像是一个纯粹的问题.
练习场景是我有两场比赛:
我在这里代表第一场比赛:
$scope.match1 = {
p1: "Player A",
p2: "Player B",
winner: "to be determined"
};
Run Code Online (Sandbox Code Playgroud)
当我设置第二场比赛时,我声明一个变量,其中包含对比赛1获胜者的引用,如下:
$scope.match2 = {
p1: $scope.match1.winner,
p2: "Player C",
winner: "tbd"
};
Run Code Online (Sandbox Code Playgroud)
现在我有一个按钮点击,它指定一个赢家匹配1,但是这个值没有通过匹配2(match2.p1值保持"待确定",尽管它引用了match1.winner,现在更新).
是什么赋予了?在此先感谢您的帮助或贡献!
那是因为你的winner字段不是字符串object.而你无法获得String的引用.这将是价值.
有winner场作为object,所以当你会改变你winner将在被改变的价值match2也.
尝试像波纹管一样的东西.
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
};
}
Run Code Online (Sandbox Code Playgroud)