4

我想在表格中实现一个功能,用户可以通过单击它来设置单元格的值。可以说有 3-4 个状态,还附有一个 ng-model。

我在 angularjs 中寻找切换按钮,但它们只是开/关类型。

简而言之; 单击该按钮会将值设置为:Active、Inactive、Excluded 寻找具有多种状态的解决方案。对此的任何帮助都非常感谢。

4

3 回答 3

8

检查以下工作示例:

http://jsfiddle.net/vishalvasani/ZavXw/9/

和控制器代码

function MyCtrl($scope) {

    $scope.btnarr=0;
    $scope.btnTxt=["Active","Inactive","Excluded"]
    $scope.change=function(){
        switch($scope.btnarr)
        {
            case 0:
                $scope.btnarr=1;
               break;
            case 1:
                 $scope.btnarr=2
               break;
            case 2:
                 $scope.btnarr=0;
               break;                
        }
    }
}

或者

较短版本的控制器

function MyCtrl($scope) {

    $scope.btnarr=0;
    $scope.btnTxt=["Active","Inactive","Excluded"]
    $scope.change=function(){
      $scope.btnarr = ($scope.btnarr + 1) % $scope.btnTxt.length;
    }
}

和 HTML

<div ng-controller="MyCtrl">
    <button ng-modle="btnarr" ng-Click="change()">{{btnTxt[btnarr]}}</button>
</div>
于 2013-09-03T12:49:09.587 回答
3

没什么。

当我在 Angular 中制作菜单时,在每个项目上,我都会有一个“选择”功能,然后从列表中选择那个特定的对象......

制作可迭代按钮更加流畅:

var i = 0;
$scope.states[
    { text : "Active" },
    { text : "Inactive" },
    { text : "Excluded" }
];

$scope.currentState = $scope.states[i];

$scope.cycleState = function () {
    i = (i + 1) % $scope.states.length;
    $scope.currentState = $scope.states[i];
    // notify services here, et cetera
}


<button ng-click="cycleState">{{currentState.text}}</button>

实际的状态数组甚至不需要成为$scope此处的一部分,如果这是您使用这些对象的唯一地方——那么您需要的唯一对象$scope将是currentState,您在何时设置你调用cycleState方法。

于 2013-09-03T12:47:30.597 回答
2

这里有两种可能性:从列表中选择状态或通过单击按钮本身来循环。

http://jsfiddle.net/evzKV/4/

JS 代码如下所示:

angular.module('test').directive('toggleValues',function(){
return {
    restrict: 'E',
    replace: true,
    template: '<div>Set Status:<div ng-repeat="value in values" class="status" ng-click="changeTo($index)">{{value}}</div><span ng-click="next()">Current Status (click to cycle): {{values[selectedValue]}}</span></div>',
    controller: ['$scope', '$element', function ($scope, $element) {
        $scope.values = ["Active", "Inactive", "Pending"];
        $scope.changeTo = function (index) {
            $scope.selectedValue = (index < $scope.values.length) ? index : 0;
        };

        $scope.next = function () {
            $scope.selectedValue = ($scope.selectedValue + 1) % $scope.values.length;
            // the modulo is stolen from Norguard (http://stackoverflow.com/a/18592722/2452446) - brilliant idea
        };

        $scope.selectedValue = 0;
    }]
};
});

HTML:

<div ng-app="test">
    <toggle-values></toggle-values>
</div>
于 2013-09-03T13:14:01.857 回答