在指令中定义范围并在视图中使用它

时间:2017-05-17 04:46:23

标签: javascript angularjs angularjs-directive

我正在使用angular-treeview directive。在这里,加载树需要一些时间,所以我想在那个时候展示一个微调器。所以,我想在指令中定义一个变量,并在视图中使用,以便我可以显示一个微调器。

**angular-treeview.js**

(function ( angular ) {
    'use strict';

    angular.module( 'angularTreeview', [] ).directive( 'treeModel', ['$compile', function( $compile) {
            return {
                restrict: 'A',
                link: function ( scope, element, attrs ) {
                    //tree id
                    var treeId = attrs.treeId;

                    //tree model
                    var treeModel = attrs.treeModel;

                    //node id
                    var nodeId = attrs.nodeId || 'id';

                    //node label
                    var nodeLabel = attrs.nodeLabel || 'label';

                    //node class
                    var nodeClass = attrs.nodeClass || 'type';

                    //children
                    var nodeChildren = attrs.nodeChildren || 'children';

                    //search by label
                    var searchQuery = attrs.searchQuery || '';

                    //function for highlighting search text
                    var searchHighlightFunction = attrs.searchHighlightFunction ||
                        function (content, text) {
                            return content
                        };

                    //css class to be added for highlighting
                    var searchHighlightClassName = attrs.searchHighlightClassName || '';

                    //filter to be used with search query
                    var searchFilter = attrs.searchFilter || '';

                    //tree template
                    var template =
                        '<ul>' +
                            '<li data-ng-repeat="node in ' + treeModel + '| ' + searchFilter + ':' + searchQuery + '" data-ng-init="node.collapsed=true">' +
                                '<i class="collapsed  {{node.' + nodeClass + '}}" data-ng-show="node.' + nodeChildren + '.length && node.collapsed" data-ng-click="' + treeId + '.selectNodeHead(node)"></i>' +
                                '<i class="expanded {{node.' + nodeClass + '}}" data-ng-show="node.' + nodeChildren + '.length && !node.collapsed" data-ng-click="' + treeId + '.selectNodeHead(node)"></i>' +
                                '<i class="normal {{node.' + nodeClass + '}}" data-ng-hide="node.' + nodeChildren + '.length"></i> ' +
                                '<span data-ng-class="node.selected" data-ng-click="' + treeId + '.selectNodeLabel(node)" ' +
                                    'ng-bind-html="' + searchHighlightFunction + '(node.' + nodeLabel + ', ' + searchQuery + ', \'' + searchHighlightClassName + '\', true)"></span>' +
                                '<div data-ng-hide="node.collapsed" data-tree-id="' + treeId + '" data-tree-model="node.' + nodeChildren +
                                    '" data-node-id=' + nodeId + ' data-node-label=' + nodeLabel + ' data-node-children="' + nodeChildren + '"' +
                                    ' data-node-class="' + nodeClass + '" data-search-query="' + searchQuery + '" data-search-highlight-function="' + searchHighlightFunction + '"' +
                                    ' data-search-highlight-class-name="' + searchHighlightClassName + '" data-search-filter="' + searchFilter + '"></div>' +
                            '</li>' +
                        '</ul>';


                    //check tree id, tree model
                    if( treeId && treeModel ) {
                        /*
                        * to expand or collapse nodes on search text changes
                        */
                        scope.$watch(searchQuery, function (newVal, oldVal) {
                            var node = scope.node;
                            if (newVal) {
                                if (newVal.length > 0) {
                                    if (node) {
                                        if ((node[nodeChildren] && node[nodeChildren].length)
                                            || (node[nodeChildren] && node[nodeChildren].length)) {
                                            node.collapsed = false;
                                        }
                                    }
                                }
                            } else {
                                if (node && ((node[nodeChildren] && node[nodeChildren].length)
                                    || (node[nodeChildren] && node[nodeChildren].length))) {
                                    node.collapsed = (scope[treeId].expandedNodes.indexOf(node[nodeLabel]) < 0);
                                }
                            }
                        });

                        //root node
                        if( attrs.angularTreeview ) {
                            //create tree object if not exists
                            scope[treeId] = scope[treeId] || {};

                            //this is where we are storing nodes by user to distinguish between
                            // those expanded by user from those done for showing search results
                            scope[treeId].expandedNodes = [];
                            scope.$watch(treeModel, function(newVal, oldVal) {
                                if (newVal) {
                                    scope[treeId].expandedNodes = [];
                                }
                            });

                            //if node head clicks,
                            scope[treeId].selectNodeHead = scope[treeId].selectNodeHead || function( selectedNode ){
                                var expandedNodesIndex;

                                //Collapse or Expand
                                selectedNode.collapsed = !selectedNode.collapsed;
                                expandedNodesIndex = scope[treeId].expandedNodes.indexOf(selectedNode[nodeLabel]);
                                if (expandedNodesIndex > -1) {
                                    if (selectedNode.collapsed) {
                                        scope[treeId].expandedNodes.splice(expandedNodesIndex, 1);
                                    }
                                } else {
                                    if (!selectedNode.collapsed) {
                                        scope[treeId].expandedNodes.push(selectedNode[nodeLabel]);
                                    }
                                }
                            };

                            //if node label clicks,
                            scope[treeId].selectNodeLabel = scope[treeId].selectNodeLabel || function( selectedNode ){

                                //remove highlight from previous node
                                if( scope[treeId].currentNode && scope[treeId].currentNode.selected ) {
                                    scope[treeId].currentNode.selected = undefined;
                                }

                                //set highlight to selected node
                                selectedNode.selected = 'selected';

                                //set currentNode
                                scope[treeId].currentNode = selectedNode;
                            };
                        }

                        //Rendering template.
                        element.html('').append( $compile( template )( scope ) );
                    }
                }
            };
        }]);
})( angular );

我的HTML是 -

<div data-angular-treeview="true" data-tree-id="Treevalue"
                                data-tree-model="suggestionList" data-node-id="id" data-node-class="type" data-node-label="name"
                                data-node-children="childrens" data-search-query="suggestionListSearchText" data-search-highlight-function="highlightHTML"
                                data-search-highlight-class-name="searchText" data-search-filter="NameFilter">
                            </div>

那么,如何在该指令中定义变量并在范围内使用它?

1 个答案:

答案 0 :(得分:0)

查看代码,可能就像在div中添加带有data-tree-view的HTML一样简单。该HTML将作为树视图指令的一部分进行擦除,因此您在其中添加的任何内容(如微调器)都将显示,直到指令完成。

编辑:

在树视图div中添加图像。此HTML将被清除,因此只会在加载时显示。

<div data-angular-treeview="true" data-tree-id="Treevalue"
                            data-tree-model="suggestionList" data-node-id="id" data-node-class="type" data-node-label="name"
                            data-node-children="childrens" data-search-query="suggestionListSearchText" data-search-highlight-function="highlightHTML"
                            data-search-highlight-class-name="searchText" data-search-filter="NameFilter">
    <img src="spinner.gif">
</div>