简体   繁体   English

将 Bootstrap 单选按钮绑定到 Angular 属性

[英]Bind Bootstrap Radio Button To Angular Property

I am having troubles getting a bootstrap radio box selection to bind into an angular property.我在将引导单选框选择绑定到角度属性时遇到了麻烦。

Here is what I have, it is not working.这是我所拥有的,它不起作用。

HTML BOOTSTRAP HTML 引导程序

 <div class="btn-group" data-toggle="buttons">
          <label class="btn btn-success active">
            <input type="radio" value="bill" ng-model="newItemType" name="options" id="option1" autocomplete="off" checked> Insert New Bill <span class="glyphicon glyphicon-file"></span> 
          </label>
          <label" class="btn btn-success ">
            <input type="radio" value="coin" ng-model="newItemType" name="options" id="option2" autocomplete="off"> Insert New Coin <span class="glyphicon glyphicon-adjust"></span>  
          </label>
        </div>

ANGULAR JAVASCRIPT角度的 JAVASCRIPT

var CurrencyInventoryBillController = function($scope, $http)
{
    $scope.newItemType = "NOT SELECTED";

    //TRIGGERED ON BUTTON CLICK. ALERT SHOWS 'NOT SELECTED' REGARDLESS OF WHICH RADIO BTN IS SELECTED
    $scope.insertNewCurrencyItem = function()
    {
        alert($scope.newItemType);
    }

}

Why is the VALUE and NG-MODEL directives not changing the SCOPE variable in this case?在这种情况下,为什么 VALUE 和 NG-MODEL 指令不更改 SCOPE 变量?

I aborted trying to get the bindings to work, and went with another approach, changing the values in the angular scope via the ng-click method inline in the html我放弃了让绑定工作的尝试,并采用了另一种方法,通过 html 中的 ng-click 方法内联更改角度范围中的值

    <div class="btn-group" data-toggle="buttons">
       <label class="btn btn-success" ng-class="{'active':newItemType=='bill'}" ng-click="newItemType='bill'" > 
         <input type="radio"> Insert New Bill <span class="glyphicon glyphicon-file"></span> 
      </label> 
      <label class="btn btn-success" ng-class="{'active':newItemType=='coin'}" ng-click="newItemType='coin'" >
        <input type="radio"> Insert New Coin <span class="glyphicon glyphicon-adjust"></span>
      </label>
    </div>

Notice the ng-click event sets the newItemType to coin or bill.请注意 ng-click 事件将 newItemType 设置为 coin 或 bill。 This approach is imperfect, but it works for now until someone can figure out how to bind radio buttons.这种方法并不完美,但它目前有效,直到有人能弄清楚如何绑定单选按钮。

Can you try this你能试试这个吗

$scope.newItemType = 'bill';

<div class="btn-group" data-toggle="buttons">
   <label class="btn btn-success" ng-class="{'active':newItemType=='bill'}" > 
     <input type="radio" value="bill" ng-model="newItemType"> Insert New Bill <span class="glyphicon glyphicon-file"></span> 
  </label> 
  <label class="btn btn-success" ng-class="{'active':newItemType=='coin'}" >
    <input type="radio" value="coin" ng-model="newItemType"> Insert New Coin <span class="glyphicon glyphicon-adjust"></span>
  </label>
</div>

Update: Here is the link to a fiddle更新:这是小提琴的链接

 var app = angular.module('myApp', []); app.controller('MainCtrl', function($scope) { });
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" /> <div ng-app="myApp"> <div ng-controller="MainCtrl"> <div class="btn-group" data-toggle="buttons"> <label class="btn btn-success" ng-class="{'active':newItemType=='bill'}"> <input type="radio" value="bill" ng-model="newItemType" name="options">Insert New Bill <span class="glyphicon glyphicon-file"></span> </label> <label class="btn btn-success" ng-class="{'active':newItemType=='coin'}"> <input type="radio" value="coin" ng-model="newItemType" name="options">Insert New Coin <span class="glyphicon glyphicon-adjust"></span> </label> </div> <br/> {{newItemType}} </div> </div>

You have it you just have to change your class active to actually see it.你有它,你只需要改变你的课程active来实际看到它。

   <div class="btn-group" data-toggle="buttons">
      <label class="btn btn-success" ng-class="{'active':newItemType=='bill'}">
        <input type="radio" value="bill" ng-model="newItemType" name="options" id="option1" autocomplete="off" checked> Insert New Bill <span class="glyphicon glyphicon-file"></span> 
      </label>
      <label class="btn btn-success" ng-class="{'active':newItemType=='coin'}">
        <input type="radio" value="coin" ng-model="newItemType" name="options" id="option2" autocomplete="off"> Insert New Coin <span class="glyphicon glyphicon-adjust"></span>  
      </label>
    </div>

 <br/>
 {{newItemType}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM