簡體   English   中英

已檢查HTML單選,無法與AngularJS指令ngModel一起使用

[英]HTML radio checked, not working along side AngularJS directive ngModel

我正在嘗試為單選按鈕設置默認選擇,但是當與側面AngularJS指令ngModel一起使用時,它不起作用

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

這是演示

在這種情況下,您可以使用ng-checked。 更新小提琴

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注釋進行更新:

小提琴

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>

您需要在控制器中初始化狀態范圍屬性:

HTML:

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

JS:

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

DEMO

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM