Jquery 对象数组参数的角度js单选按钮

Jquery 对象数组参数的角度js单选按钮,jquery,asp.net-mvc,html,angularjs,Jquery,Asp.net Mvc,Html,Angularjs,我是angular js新手,在为c#list属性的单选按钮选择默认值时遇到问题 我有小提琴链接,代码如下 HTML: } 我需要帮助的是: 我希望在加载页面时选择默认值 如果管理员更改选项,则数据绑定 我不确定小提琴是如何工作的,所以我将在这里发布解决方案。你的问题有两个方面 为单选按钮应用数据绑定的步骤 用值初始化 虽然1号应该照顾1号,但对我来说不是。也许有人会有更好的解决办法。我使用ng模式进行数据绑定,并使用带有超时的自定义指令初始化值。代码如下 var first2 = ang

我是angular js新手,在为c#list属性的单选按钮选择默认值时遇到问题

我有小提琴链接,代码如下

HTML:

}

我需要帮助的是:

  • 我希望在加载页面时选择默认值
  • 如果管理员更改选项,则数据绑定

我不确定小提琴是如何工作的,所以我将在这里发布解决方案。你的问题有两个方面

  • 为单选按钮应用数据绑定的步骤
  • 用值初始化
  • 虽然1号应该照顾1号,但对我来说不是。也许有人会有更好的解决办法。我使用ng模式进行数据绑定,并使用带有超时的自定义指令初始化值。代码如下

    var first2 = angular.module('first22', []);
        first2.controller('myCtrl', [ '$scope', function ($scope) {
            $scope.data = [{
                "QuestionId":1,
                "QuestionDescription": "What is your fav color?",
                "Options":[{"Id":1,"OptionDescription":"White","IsAnswer":true},
                           {"Id":2,"OptionDescription":"Black","IsAnswer":false}]
            }];
        }])
        first2.directive('updateStatus',['$timeout',function($timeout)
        {
            return{
                restrict:'A',
                scope: true,
                link: function(scope, element, attrs) {
                    $timeout(function(){
                        var stat = attrs.updateStatus.toLowerCase() == "true" ? true : false;
                        $(element)[0].checked = stat;
                        }, 0, false);
                },
            }
        }]);
    
    HTML

    
    问题:

    • {{问题.问题描述} {{i.OptionDescription} 对 不 --{{i.IsAnswer}}
    为什么我要给你定制指令?我使用了ng selected,但我相信,在数据绑定上,所选值会被重置,从而在自定义指令中超时。

    找到了解决方案: 更新了小提琴链接
    1)对于选中默认无线电的自动绑定数据:

    分配单选选项的“value”属性(在我的例子中,它是一个布尔值,关联为“1”和“0”),然后将服务器值中的“ngModel”角度指令属性值分配为(ngModel=“i.IsAnswer”)

    现在您的html应该如下所示,在任何更改时,通过单击事件触发上述事件

    
    {{i.QuestionDescription}}
    
    • {{o.OptionDescription} 正确答案是: 对 不
    enter code herefunction MyCtrl($scope) {
    $scope.data = [{
        "QuestionId":1,
        "QuestionDescription": "What is your fav color?",
        "Options":[{"Id":1,"OptionDescription":"White","IsAnswer":true},
                   {"Id":2,"OptionDescription":"Black","IsAnswer":false}]
    }];
    
    var first2 = angular.module('first22', []);
        first2.controller('myCtrl', [ '$scope', function ($scope) {
            $scope.data = [{
                "QuestionId":1,
                "QuestionDescription": "What is your fav color?",
                "Options":[{"Id":1,"OptionDescription":"White","IsAnswer":true},
                           {"Id":2,"OptionDescription":"Black","IsAnswer":false}]
            }];
        }])
        first2.directive('updateStatus',['$timeout',function($timeout)
        {
            return{
                restrict:'A',
                scope: true,
                link: function(scope, element, attrs) {
                    $timeout(function(){
                        var stat = attrs.updateStatus.toLowerCase() == "true" ? true : false;
                        $(element)[0].checked = stat;
                        }, 0, false);
                },
            }
        }]);
    
    <body ng-app="first22">
    <div ng-controller="myCtrl">
        <p>Questions:</p>
        <ul>
            <li ng-repeat="question in data" >
                <label>{{question.QuestionDescription}}</label>
                <div ng-repeat="i in question.Options">
                    {{i.OptionDescription}}
                    <input type="radio" name="QuestionAnswer{{$index}}" ng-model="i.IsAnswer" value="true" update-status="{{i.IsAnswer}}"/>Yes
                    <input type="radio" name="QuestionAnswer{{$index}}" ng-model="i.IsAnswer" value="false" update-status="{{!i.IsAnswer}}"/>No
                    -- {{i.IsAnswer}}
                </div>
            </li>
        </ul>
    </div>
    <script type="text/javascript" src="lib/jquery/jquery.js"></script>
    <script type="text/javascript" src="lib/angular/angular.js"></script>
    </body>
    
    <div ng-controller="MyCtrl">
    <p>Questions:</p>
    <ul>
        <li ng-repeat="question in data">
            <label>{{question.QuestionDescription}}</label>
            <div ng-repeat="i in question.Options">
                {{i.OptionDescription}}
                <input type="radio" ng-model="i.IsAnswer" value="1" name="QuestionAnswer{{i.Id}}"/>Yes
                <input type="radio" ng-model="i.IsAnswer" value="0" name="QuestionAnswer{{i.Id}}"/>No
            </div>
        </li>
    </ul>
    
        // update existing question
    $scope.updateQuestion = function (index) {
        var question = JSON.stringify($scope.data[index]);
        $http({
            method: 'POST',
            url: "/Admin/SaveQuestion",
            data: "question="+question,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).
        then(function (result) {
            $window.location = "#/";
        },
        function () {
            //error
            alert("question could not be saved");
        });
    };
    
    <div class="question row" data-ng-repeat="i in data">
            <div class="q-description span5">
                <h3>{{ i.QuestionDescription }}</h3><a href="javascript:void(0);" ng-click="updateQuestion($index);">Update Question</a>
            </div>
            <div class="option span6">
                <ul class="o-content">
    
                    <li class="o-description" data-ng-repeat="o in i.Options">
                        <div class="o-d-text">{{ o.OptionDescription }}</div>
                        <div class="o-d-option">
                            Is Right Answer:
                            <input type="radio" name="optionSelected{{o.Id}}" ng-model="o.IsAnswer" value="1" />Yes
                            <input type="radio" name="optionSelected{{o.Id}}" ng-model="o.IsAnswer" value="0" />No
                        </div>
                    </li>
    
                </ul>
            </div>
        </div>