已检查HTML广播,但未与AngularJS指令ngModel一起使用

时间:2015-01-29 10:23:26

标签: html angularjs

我尝试为单选按钮设置默认选项,但在AngularJS指令ngModel

一侧使用时,它无法正常工作
<input type="radio" value="5000" checked ng-model="status">5000

以下是DEMO

2 个答案:

答案 0 :(得分:1)

在这种情况下,您可以使用ng-checked。 Updated Fiddle

HTML代码:

<div ng-app="myApp">
    <div ng-controller="test">
        <input type="radio" value="5000" ng-checked="status">5000
        <br/> <span>{{status}}</span>

    </div>
</div>

JS代码:

angular.module("myApp", [])

    .controller("test", function ($scope) {
    $scope.status = true;
});

根据OP评论更新:

fiddle

JS代码:

angular.module("myApp", [])

    .controller("test", function ($scope) {
    $scope.status = true;
    if (!$scope.status) $scope.radioValue = 0;
    else $scope.radioValue = 5000;
});

HTML代码:

<div ng-app="myApp">
    <div ng-controller="test">
        <input type="radio" value="5000" ng-checked="status" ng-model="radioValue">5000
        <br/> <span>{{radioValue}}</span>

    </div>
</div>

答案 1 :(得分:0)

您需要初始化控制器中的状态范围属性:

HTML:

<input type="radio" value="5000" checked ng-model="status">5000

JS:

angular.module("myApp", []).controller("test", function ($scope) {
    $scope.status = 5000;
});

DEMO

相关问题