将引导单选按钮绑定到角度属性
Bind Bootstrap Radio Button To Angular Property
我在获取绑定到角度属性的引导程序单选框选择时遇到问题。
这是我的东西,它不起作用。
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>
角度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);
}
}
在这种情况下,为什么VALUE和NG-MODEL指令不更改SCOPE变量?
我放弃了让绑定工作的尝试,并采用了另一种方法,通过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>
请注意,ng-click事件将newItemType设置为coin或bill。这种方法并不完美,但在有人想出如何绑定单选按钮之前,它暂时有效。
你能试试这个吗
$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>
更新:这是一个小提琴的链接
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>
你有了它,你只需要改变你的类别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}}
相关文章:
- IE8中的Ember.js(1.0pre)属性绑定错误
- LiveCycle Xml属性绑定
- angular2中具有属性绑定的字符串串联
- Javascript数据将元素属性绑定到URL中的锚href
- 从服务器端将属性绑定到套接字对象;似乎不能正常工作
- AngularJs 将模型属性绑定到复选框
- 属性绑定中的 Javascript 或 Query in Birt
- AngularJS指令将名称属性绑定到模板元素
- React JS - uncatch TypeError:无法读取未定义的属性“绑定”
- ng-options 模型基于属性绑定
- ReactJS 无法读取未定义的属性“绑定”
- 使用$scope属性绑定ng-includ-src
- 使用Knockout.js如何将Date属性绑定到HTML5日期选择器
- AngularJS:将角度服务属性绑定到范围
- 为什么可以't我使用原型将属性绑定到对象
- AngularJs:将作用域对象的属性绑定到指令
- 通过公共属性绑定指令实例
- EmberJS属性绑定
- 当使用映射时,属性绑定被破坏
- Angularjs属性绑定优先级