如何使AngularJS示例正常工作?

时间:2015-08-10 06:01:54

标签: javascript angularjs

我发现这个示例的魔杖我想在我的教程项目中使用它。

问题在于此



'use strict';

var app = angular.module('myApp', ['app.directives']);

app.controller('AppCtrl', function($scope){                     
    $scope.roles = [
          {"id": 1, "name": "Manager", "assignable": true},
          {"id": 2, "name": "Developer", "assignable": true},
          {"id": 3, "name": "Reporter", "assignable": true}
    ];
    
    $scope.member = {roles: []};
    $scope.selected_items = [];
});

var app_directives = angular.module('app.directives', []);

app_directives.directive('dropdownMultiselect', function(){
   return {
       restrict: 'E',
       scope:{           
            model: '=',
            options: '=',
            pre_selected: '=preSelected'
       },
       template: "<div class='btn-group' data-ng-class='{open: open}'>"+
        "<button class='btn btn-small'>Select</button>"+
                "<button class='btn btn-small dropdown-toggle' data-ng-click='open=!open;openDropdown()'><span class='caret'></span></button>"+
                "<ul class='dropdown-menu' aria-labelledby='dropdownMenu'>" + 
                    "<li><a data-ng-click='selectAll()'><i class='icon-ok-sign'></i>  Check All</a></li>" +
                    "<li><a data-ng-click='deselectAll();'><i class='icon-remove-sign'></i>  Uncheck All</a></li>" +                    
                    "<li class='divider'></li>" +
                    "<li data-ng-repeat='option in options'> <a data-ng-click='setSelectedItem()'>{{option.name}}<span data-ng-class='isChecked(option.id)'></span></a></li>" +                                        
                "</ul>" +
            "</div>" ,
       controller: function($scope){
           
           $scope.openDropdown = function(){        
                    $scope.selected_items = [];
                    for(var i=0; i<$scope.pre_selected.length; i++){                        $scope.selected_items.push($scope.pre_selected[i].id);
                    }                                        
            };
           
            $scope.selectAll = function () {
                $scope.model = _.pluck($scope.options, 'id');
                console.log($scope.model);
            };            
            $scope.deselectAll = function() {
                $scope.model=[];
                console.log($scope.model);
            };
            $scope.setSelectedItem = function(){
                var id = this.option.id;
                if (_.contains($scope.model, id)) {
                    $scope.model = _.without($scope.model, id);
                } else {
                    $scope.model.push(id);
                }
                console.log($scope.model);
                return false;
            };
            $scope.isChecked = function (id) {                 
                if (_.contains($scope.model, id)) {
                    return 'icon-ok pull-right';
                }
                return false;
            };                                 
       }
   } 
});
&#13;
<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="AppCtrl">    
    <dropdown-multiselect pre-selected="member.roles" model="selected_items" options="roles"></dropdown-multiselect>
    
    
    <pre>selected roles = {{selected_items | json}}</pre>
</div>
&#13;
&#13;
&#13;

属性定义为硬编码:

  name
    id

因此,如果我的字段名称调用另一个,则该示例将不起作用。

我知道如果字段调用不同的名称,我的代码中有哪些更改可以使示例正常工作?

2 个答案:

答案 0 :(得分:2)

虽然我认为强制输入自己使用确定的密钥作为API的一部分是完全正确的,但通过将其他参数传递到指令中,完全可以自定义它。

.directive(function(){
    return {
        scope: {
            options: '=',
            keys: '='
        },
        template: "<div ng-repeat='option in options'> \
                {{option[key.id]}} - {{option[key.name]}} \
            <div>",
        controller: function($scope){
            // default if key is not specified
            if (!$scope.keys) $scope.keys = {id: 'id', name: 'name'};

            angular.forEach($scope.options, function(option){
                // refer to id by option[$scope.key.id]
                // refer to name by option[$scope.key.name]
            });
        }
    }
})

当你转入指令时,你需要告诉它哪个键分别是id和name

$scope.test = [
    {serial: 123, product: 'apple'},
    {serial: 124, product: 'orange'}
]

$scope.keys = {id: 'serial', name: 'product'};

<dropdown-multiselect model="selected_items" options="test" keys="keys"></dropdown-multiselect>

答案 1 :(得分:2)

<script src="http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.4/underscore-min.js"></script>
<link href="http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.0/css/bootstrap-combined.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="AppCtrl">    
    <dropdown-multiselect pre-selected="member.roles" model="selected_items"  displayname ="displayname" options="roles"></dropdown-multiselect>
    
    
    <pre>selected roles = {{selected_items | json}}</pre>
</div>
function deepCloneStore (source) {
    var target = Ext.create ('Ext.data.Store', {
        model: source.model
    });
    Ext.each (source.getRange (), function (record) {
        var newRecordData = Ext.clone (record.copy().data);
        var model = new source.model (newRecordData, newRecordData.id);
        target.add (model);
    });
    return target;
}