【问题标题】:Angularjs : directive recursive in selectAngularjs:选择中的指令递归
【发布时间】:2016-03-30 15:29:01
【问题描述】:

我的指令递归构建选择输入有问题。

我有一些模块有一些功能,这些功能可以有一些像这样的子功能:

[
  0: {
    module_name : administration
    fonctions : [
       {
         fonction_id : 1
         fonction_name : getAdministration
         children : [
            {
              fonction_id : 2
              fonction_name : getParameters
              children : []
            },
            {
              fonction_id : 3
              fonction_name : getModules
              children : []
            },
            ...
         ]
       },
    ]
  },
  1: {
    module_name : Account
    fonctions : [
       {
         fonction_id : 4
         fonction_name : CreateAccount
         children : []
       },
       {
         fonction_id : 5
         fonction_name : EditAccount
         children : []
       },
       ....
    ]
  },
  ...
]

目标是为每个模块在select中构建optgroup,并在option中放入该模块的每个功能,如果功能有孩子,继续同一个 optgroup 否则创建一个新的 optgroup 来获得这个结果:

<select>
  <optgroup label='Administration'>
    <option value='1'>getAdministration</option>
    <option value='2'>getParameters</option>
    <option value='3'>getModules</option>
    ...
  </optgroup>
  <optgroup label='Account'>
    <option value='1'>CreateAccount</option>
    <option value='2'>EditAccount</option>
    ...
  </optgroup>
</select>

为了制定这些指令,我受到这篇文章的启发:http://sporto.github.io/blog/2013/06/24/nested-recursive-directives-in-angular/

看我的两条指令:

WcSelectOptionGroup:

angular.module('app.administration').directive('wcSelectOptionGroup', function()
{
    return {
        restrict: 'E',
        replace: true,
        scope: {
            elts: '=',
            selectId: '='
        },
        template: '<select><optgroup' +
                        'label="{{ elt.module_name }}"' +
                        'ng-repeat="elt in elts">' +
                            '<wc-select-option' +
                                'ng-repeat="fnct in elt.fonctions"' +
                                'ng-selected="fnct.fonction_id === selectId"' +
                                'item="fnct"' +
                                'selectId="selectId">' +
                            '</wc-select-option>' +
                  '</optgroup></select>',
        link: function(scope)
        {
            console.log('hello 1!!!!!!!');
        }
    };
});

第二个:

WcSelectOption:

angular.module('app.administration').directive('wcSelectOption', function($compile)
{
    return {
        restrict: 'E',
        replace: true,
        scope: {
            item: '=',
            selectId: '='
        },
        template: '<option' +
                        'value="{{ item.fonction_id }}"' +
                        'ng-selected="item.fonction_id === selectId">' +
                        '{{ item.fonction_title }}' +
                   '</option>',
        link: function(scope, element, attrs)
        {
            console.log('hello 2!!!!!!!');
            angular.forEach(scope.item.children, function(child)
            {
                $compile('<wc-select-option' +
                            'item="child"' +
                            'selectId="selectId">' +
                            '</wc-select-option>')
                (scope, function(cloned, scope)
                {
                    element.append(cloned);
                });
            });
        }
    };
});

和 html :

<select
       class="form-control"
       name="parent"
       ng-model="fonction.fonction_parent_id"
       required>
           <option
               value="0"
               ng-selected="fonction.fonction_parent_id === null">
                 Aucun
           </option>
           <optgroup label="{{ $root.getWord('Functions not assigned') }}">
               <option
                   ng-repeat="fnct in fonctions.not_assigned"
                   value="{{ fnct.fonction_id }}"
                   ng-selected="fnct.fonction_id===fonction.fonction_parent_id">
                     {{ fnct.fonction_name }}
               </option>
           </optgroup>
           <wc-select-option-group
               elts="modules"
               selectId="fonction.fonction_parent_id">
           </wc-select-option-group>
</select>

当我运行应用程序时,指令 WcSelectOptionGroup 不会执行,因为当我放置一个 console.log('hello 1!!!') 时,什么都没有发生...... 我不明白为什么。

所以如果你有一些想法:) 提前致谢

【问题讨论】:

    标签: javascript angularjs recursion angularjs-directive


    【解决方案1】:

    我终于找到了解决办法:)

    我做了 3 个指令:WcSelect、WcSelectOptionGroup 和 WcSelectOption

    查看指令:

    WcSelect

    angular.module('app.administration').directive('wcSelect', ['$compile', function($compile)
    {
        return {
            restrict: 'E',
            replace: true,
            scope: {
                modules: '=',
                fnctsNotAssigned: '=',
                fnctModel : '='
            },
            templateUrl: 'app/_common/directives/wc-select.tpl.html',
            link: function(scope, element, attrs)
            {
                scope.$watch('modules', function(newValue)
                {
                    if (angular.isArray(newValue) &&
                        newValue.length > 0)
                    {
                        angular.element('.optGrpFunctions').remove();
    
                        var str = '<wc-select-option-group ng-repeat="module in modules" elt="module" select-id="parentId"></wc-select-option-group>';
                        $compile(str)(scope, function(cloned, scope) { element.append(cloned); });
                    }
                });
    
                scope.$watch('fnctModel', function(newValue)
                {    
                    if (newValue.fonction_parent_id !== null)
                    {
                        angular.element('.optGrpFunctions').remove();
                        scope.parentId = newValue.fonction_parent_id;
    
                        if (angular.isArray(scope.modules) &&
                          scope.modules.length > 0)
                        {
                            var str = '<wc-select-option-group ng-repeat="module in modules" elt="module" select-id="parentId"></wc-select-option-group>';
                            $compile(str)(scope, function(cloned, scope) { element.append(cloned); });
                        }
                    }
                });
    
                scope.parentId = null;
            }
        };
    }]);
    

    关联的模板:

    <select
            class="form-control"
            name="parent"
            ng-model="fnctModel.fonction_parent_id"
            required>
        <option
            value="0"
            ng-selected="fnctModel.fonction_parent_id === null">
                Aucun
        </option>
        <optgroup
            label="{{ $root.getWord('Functions not assigned') }}"
            ng-if="fnctsNotAssigned.length > 0">
            <option
                ng-repeat="fnct in fnctsNotAssigned"
                value="{{ fnct.fonction_id }}"
                ng-selected="fnct.fonction_id === fnctModel.fonction_parent_id">
                {{ fnct.fonction_name }}
            </option>
        </optgroup>
    
    </select>
    

    WcSelectOptionGroup

    angular.module('app.administration').directive('wcSelectOptionGroup', ['$compile', function($compile)
    {
        return {
            restrict: 'E',
            replace: true,
            scope: {
                elt: '=',
                selectId: '='
            },
            template: '<optgroup class="optGrpFunctions" label="{{ elt.module_name }}"></optgroup>',
            link: function(scope, element, attrs)
            {
                /**
                 * ajoute les fonctions de elt a l'optGroup
                 *
                 * @author tibo
                 */
                scope.$watch('elt.fonctions', function(newValue)
                {
                    if (angular.isArray(newValue) &&
                       newValue.length > 0)
                    {
                        var str = '<wc-select-option ng-repeat="fnct in elt.fonctions" item="fnct" select-id="selectId"></wc-select-option>';
                        $compile(str)(scope, function(cloned, scope) { element.append(cloned); });
                    }
                });
            }
        };
    }]);
    

    WcSelectOption

    angular.module('app.administration').directive('wcSelectOption', ['$compile', function($compile)
    {
        return {
            restrict: 'E',
            replace: true,
            scope: {
                item: '=',
                selectId: '='
            },
            template: '<option value="{{ item.fonction_id }}" ng-selected="item.fonction_id === selectId">{{ item.fonction_title }}</option>',
            link: function(scope, element, attrs)
            {
                /**
                 * ajoute les fonctions enfants de item a loptgroup
                 *
                 * @author tibo
                 */
                scope.$watch('item.children', function(newValue)
                {
                    if (angular.isArray(newValue))
                    {
                        var str = '<wc-select-option ng-repeat="child in item.children" item="child" select-id="selectId"></wc-select-option>';
                        $compile(str)(scope, function(cloned, scope){ element.after(cloned); });
                    }
                });
            }
        };
    }]);
    

    我认为我的错误是将指令直接放在指令的模板中,而不是使用$compile来执行指令WcSelectOptionGroup。

    再见:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 2014-03-26
      • 2014-06-01
      • 2013-01-04
      • 2014-04-24
      • 2018-03-07
      • 2013-04-19
      相关资源
      最近更新 更多