2016-08-22 79 views
0

我想創建新用戶時創建選擇它將需要一個組。它工作得很好,但「檢查」選項由於某種原因不起作用。我怎樣才能使「用戶」被選中,所以如果按下新建用戶按鈕,情況會如此呢?Angularjs無線電選擇作爲檢查

<div class="radio-group" required> 
     <h3>Set user group</h3> 
    <label class="radio-inline"> 
     <input type="radio" name="groupradio" value="2" ng-model="groups.group" required> Admin 
    </label> 

    <label class="radio-inline"> 
     <input type="radio" name="groupradio" value="1" ng-model="groups.group" checked> User 
    </label> 

    <label class="radio-inline"> 
     <input type="radio" name="groupradio" value="3" ng-model="groups.group"> Viewer 
    </label> 
    </div> 
    <button class="btn-sonera" ng-click="createUser(user, groups)"><i class="fa fa-floppy-o" aria-hidden="true"></i> Create user</button> 
+0

檢查= 「檢查」 –

+0

看到這個http://stackoverflow.com/questions/23279296/radio-buttons-ng-checked-with-ng-model –

+0

我們可能缺失一些背景。正如你可以在你的代碼中看到這個運行程序(https://plnkr.co/edit/VE93DmUXIBxRYJX7mpgy?p=preview),用戶複選框被選中。 – Eric

回答

1

隨着AngularJs最好是使用它的指令:

ng-checked="variable" 
0

可否請你用NG-值

<label> 
    <input type="radio" ng-model="groups.group" value="1"> 
    Admin 
    </label><br/> 
    <label> 
    <input type="radio" ng-model="groups.group" value="2"> 
    User 
    </label><br/> 
    <label> 
    <input type="radio" ng-model="groups.group" value="3"> 
    Viewer 
    </label><br/> 

in controller

 $scope.createUser = function(user, groups){ 
     groups.group = "2"; 

     }; 
+0

沒有運氣與ng值。 – Shnigi

+0

是否有可能是這樣的(更新了答案) –

+0

我認爲它實際上也是這樣的:$ scope.groups = {group:1}; – Shnigi

0

Here is a working plunker: https://plnkr.co/edit/4VtblBSWROLTETW5Ie5C?p=preview

The core code:

$scope.groups = { 
    admin : false, 
    user: false, 
    viewer: false 
    }; 

And in the view:

<button class="btn-sonera" ng-click="groups.user=true"><i class="fa fa-floppy-o" aria-hidden="true"></i> Create user</button> 

Note how each ng-model is bound to an object, ie: groups.admin, groups.user etc.

1

I managed to make it checked with ng-init="groups.group=1"因爲我需要被分配給無線價值嘗試。這種方式使用它有什麼問題嗎?

的完整代碼

<div class="radio-group" required> 
    <h3>Set user group</h3> 
<label class="radio-inline"> 
    <input type="radio" name="groupradio" value="2" ng-model="groups.group" required> Admin 
</label> 

<label class="radio-inline"> 
    <input type="radio" name="groupradio" value="1" ng-model="groups.group" ng-init="groups.group=1"> User 
</label> 

<label class="radio-inline"> 
    <input type="radio" name="groupradio" value="3" ng-model="groups.group"> Viewer 
</label> 
</div>