0

编辑:当我在表单中更改 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>
4

2 回答 2

1

我认为这是因为 ng-repeat 为每个项目创建了一个新的子范围,并且由于selectedAnswer没有在重复项目之外初始化,因此selectedAnswer将始终undefined位于父范围内。您可以通过执行以下操作来解决此问题:

      <form ng-init="outside={}">
        <span>{{node.Question.Text}}</span>
        <div class="radio" ng-repeat="answer in node.Answers">
            <input type="radio" id="answerGroup" name="answerGroup" ng-model="outside.selectedAnswer" 
                   value="[{{answer.BranchId}},{{node.LeafId}},{{answer.Id}}]"/> {{answer.Text}}
        </div>
        <div>
            <input type="button" ng-click="next(outside.selectedAnswer)" value="Next" id="Next"/>
        </div>
      </form>
于 2013-05-24T16:13:24.597 回答
0

如果您在控制器中定义 $scope.selectedAnswer 是否可以解决问题?

app.controller('GameNode', function ($scope, $http) {
    $scope.selectedAnswer = null;
    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();
});

如果没有,请创建一个 jsFiddle 供我们使用。

于 2013-05-24T16:12:42.120 回答