编辑:当我在表单中更改 ng-model="$parent.selectedAnswer" 时,它可以工作。如果有人可以解释它,我会将其标记为答案。
我正在尝试在单击“下一步”按钮时将选定的单选按钮值收集到角度函数,但它始终未定义。我真的不知道出了什么问题。单选按钮和值在 html 中正确填充。只有在单击下一步并尝试传入所选值时才会失败。
我的逻辑很简单:每个单选按钮的值都是 [branchid, leafid, answerid] 的数组。单击下一步时,我尝试将该值绑定到“selectedAnswer”模型并将其传递给 angularjs 端的 next() 函数。
以下是相关代码:
app.controller('GameNode', function ($scope, $http) {
function load(selectedAnswer) {
var leafId = 0, branchId = 0, answerId = 0;
alert(selectedAnswer); //this is always undefined
if (selectedAnswer !== undefined && selectedAnswer != null) {
branchId = selectedAnswer[0];
leafId = selectedAnswer[1];
answerId = selectedAnswer[2];
}
$http.get("/AccountRequest/GetNode?branchId=" + branchId +
"&leafId=" + leafId +
"&answerId=" + answerId)
.success(function(data) {
$scope.node = data;
});
}
$scope.next = load;
load();
});
-------------------------------------html 方面------------ ----------------------
<div class="container-fluid" ng-app="AccountRequest" ng-controller="GameNode">
<div class="row-fluid">
<div class="span2"></div>
<div class="span10">
<form>
<span>{{node.Question.Text}}</span>
<div class="radio" ng-repeat="answer in node.Answers">
<input type="radio" id="answerGroup" name="answerGroup" ng-model="selectedAnswer"
value="[{{answer.BranchId}},{{node.LeafId}},{{answer.Id}}]"/> {{answer.Text}}
</div>
<div>
<input type="button" ng-click="next(selectedAnswer)" value="Next" id="Next"/>
</div>
</form>
</div>
</div>
</div>