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

时间:2021-04-12 13:44:09

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

 <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

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变量?

3 个解决方案

#1


1  

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

更新:这是一个小提琴的链接

#2


3  

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. This approach sucks ass balls, but it works for now until someone can figure out how to bind radio buttons.

请注意,ng-click事件将newItemType设置为coin或bill。这种方法很糟糕,但它现在有效,直到有人能弄清楚如何绑定单选按钮。

#3


1  

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.

你有它你只需要改变你的类活跃实际看到它。

   <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}}

#1


1  

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

更新:这是一个小提琴的链接

#2


3  

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. This approach sucks ass balls, but it works for now until someone can figure out how to bind radio buttons.

请注意,ng-click事件将newItemType设置为coin或bill。这种方法很糟糕,但它现在有效,直到有人能弄清楚如何绑定单选按钮。

#3


1  

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.

你有它你只需要改变你的类活跃实际看到它。

   <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}}