2016-08-22 68 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>