14

我正在尝试创建一个简单的twitter 引导按钮组,允许用户选择几个选项之一(想想单选按钮)。我已经达到了影响模型更改的程度,但是单击时未正确设置“活动”状态……除非我再次单击它?我创建了一个小提琴,基本标记如下......

<div range="justified"
    model="myModel"
    options="rangeOptions"></div>
<hr/>
<div range="vertical"
    model="myModel"
    options="rangeOptions"></div>
<hr/>

<pre>myModel:{{myModel}}</pre>

<script type="text/ng-template" id="range.tpl.html">
    <div class="btn-group btn-group-{{type}}" data-toggle="buttons">
        <span class="btn btn-lg btn-primary"
            ng-repeat="option in options"
            ng-class="{active:option.id==model.range_id}" <!-- not working?? -->
            ng-click="activate(option.id)">{{option.label}}</span>
    </div>
</script>
function Main($scope) {
    $scope.rangeOptions = [
        {id:1,label:"Agree"},
        {id:2,label:"Neutral"},
        {id:3,label:"Disagree"}
    ];
    $scope.myModel = {range_id: 2};
}
angular
    .module('range', [])
    .directive('range', function () {
        return {
            replace: true,
            scope: { type:'@range', model:'=', options:'=' },
            templateUrl:'range.tpl.html',
            controller: function ($scope,$element,$attrs) {
                $scope.activate = function (option) {
                    $event.preventDefault();
                };
            }
        };
    });
4

5 回答 5

27

你不需要 90% 的 bootstrap js 来让这样的事情工作你需要做的就是创建一个按钮组并附加一些 ng-clicks 和 ng-class 到它:

function myscope($scope) {
  $scope.button = 'red';
}

<div class="btn-group">
    <a href="javascript:void(0)" ng-click="button = 'red'" ng-class="{ 'active' : button == 'red' }" class="btn">red</a>
    <a href="javascript:void(0)" ng-click="button = 'blue'" ng-class="{ 'active' : button == 'blue' }" class="btn">blue</a>
    <a href="javascript:void(0)" ng-click="button = 'green'" ng-class="{ 'active' : button == 'green' }" class="btn">green</a>
</div>
于 2013-09-26T07:44:50.153 回答
8

这里有一些关于使您的按钮组作为单选按钮工作的附加信息。

http://getbootstrap.com/javascript/#buttons

使用该信息,我分叉了您的 Fiddle,这似乎正在工作。

http://jsfiddle.net/puatj/

更改只是将您的跨度元素切换为标签/输入。

<div class="btn-group btn-group-{{type}}" data-toggle="buttons">
    <label class="btn btn-lg btn-primary"
        ng-repeat="option in options"
        ng-class="{active:option.id==model.range_id}"
    ng-click="activate(option.id)"><input type="radio"></input>{{option.label}}</label>
</div>
于 2013-09-25T21:29:39.840 回答
4

如果你不介意放弃一些时髦的引导样式,你可以通过简单地取出 data-toggle 标签来解决这个问题

<div ng-app>
<h1>{{color}}</h1>
<form>
    <div>
        <label>Select a Color</label>
    </div>
    <div class="btn-group">
        <label class="btn btn-primary">
            <input type="radio" name="color" data-ng-model="color" value="'red'"> Red
        </label>
            <label class="btn btn-primary">
        <input type="radio" name="color" data-ng-model="color" value="'green'"> Green
            </label>
        <label class="btn btn-primary">
            <input type="radio" name="color" data-ng-model="color" value="'blue'"> Blue
        </label>
        <label class="btn btn-primary">
            <input type="radio" name="color" data-ng-model="color" value="'black'"> Black
        </label>
        <label class="btn btn-primary">
            <input type="radio" name="color" data-ng-model="color" value="'orange'"> Orange
        </label>
    </div>
</form>
</div>

你可以在这里找到 JSFiddle。

于 2013-11-02T20:46:01.557 回答
3

从父 div 中删除 data-toggle="buttons" 并在设置属性 width: 0px 和 visibility: hidden 的输入中添加一个类或样式。

要将按钮显示为活动的,请使用 ng-init、ng-click 和 ng-class 来切换活动类。

<label>Location</label><br/>
<div class="btn-group" ng-init="selectedButton = 'all'">
            <label class="btn btn-default active" ng-class="{'active':selectedButton === 'all'}" ng-click="selectedButton = 'all'">
               <input type="radio" name="inlineRadioOptions" id="inlineRadio1" value="all" ng-model="filterData.locations" checked style="visibility:hidden; width:0px;" > All Locations
            </label>
            <label class="btn btn-default" ng-class="{'active':selectedButton === 'boroughs'}" ng-click="selectedButton = 'boroughs'">
               <input type="radio" name="inlineRadioOptions" id="inlineRadio2" value="boroughs" ng-model="filterData.locations" style="visibility:hidden; width:0px;" > Boroughs
            </label>
            <label class="btn btn-default" ng-class="{'active':selectedButton === 'depots'}" ng-click="selectedButton = 'depots'">
               <input type="radio" name="inlineRadioOptions" id="inlineRadio3" value="depots" ng-model="filterData.locations" style="visibility:hidden; width:0px;" > Depots
            </label>
</div>
于 2015-07-31T15:06:01.380 回答
0

我直接从控制器中的数组中找到了处理此问题的角度方式。

<div class="btn-group" data-toggle="buttons">
    <label class="btn btn-tertiary" ng-class="{active: availableOption === searchFieldsContent.searchOption}" ng-repeat="availableOption in searchOptions">
        <input type="radio" name="searchOption" id="searchOption{{$index}}" ng-model="searchFieldsContent.searchOption" ng-value="availableOption">{{availableOption.name}}</input>
    </label>
</div>
于 2015-01-22T13:33:37.587 回答