Angular JS - 从不同文件加载模块

时间:2013-11-14 17:59:11

标签: angularjs module

我正在使用http://mgcrea.github.io/angular-strap/中的一些模块和树视图:http://ngmodules.org/modules/angular.treeview并且在加载它时遇到问题。例如,我在两个文件中拆分了treeview.js:1包含控制器,另一个包含指令(正如我在一些帖子中看到的那样,它是Angular的一个好习惯):

app.js - >加载flexylaout,模态和网格

var app = angular.module('app',['flexyLayout','ui.bootstrap','ngGrid']);

treeController.js

(function(){
    app.controller('TreeCtrl', function($scope){
    $scope.roleList1 = [
    { "roleName" : "Escapamentos e Catalisadores", "roleId" : "role1", "children" :       [          
      { "roleName" : "Silencioso Intermediario", "roleId" : "role12", "children" : [
        { "roleName" : "Composicao", "roleId" : "role121", "children" : [
          { "roleName" : "Material 1", "roleId" : "role1211", "children" : [] },
          { "roleName" : "Material 2", "roleId" : "role1212", "children" : [] }
        ]}
      ]}
    ]},

    { "roleName" : "Arquivo 1", "roleId" : "role2", "children" : [] },
    { "roleName" : "Arquivo 2", "roleId" : "role3", "children" : [] }
  ];

//test tree model 2
$scope.roleList2 = [
    { "roleName" : "Tubos", "roleId" : "role1", "children" : [
      { "roleName" : "Galvanizados", "roleId" : "role11", "collapsed" : true, "children" : [] },
      { "roleName" : "Conducao", "roleId" : "role12", "collapsed" : true, "children" : [
        { "roleName" : "Material 1", "roleId" : "role121", "children" : [
          { "roleName" : "Material 2", "roleId" : "role1211", "children" : [] },
          { "roleName" : "Material 3", "roleId" : "role1212", "children" : [] }
        ]}
      ]}
    ]}
  ];
  });
})();

directive.js

(function(){
    app.directive('treeModel',function($compile){

    return{
        restrict:"A",
        link:function(a,g,c){
            var e=c.treeModel,
            h=c.nodeLabel||"label",
            d=c.nodeChildren||"children",
            k='<ul><li data-ng-repeat="node in '
            +e+'"><i class="collapsed" data-ng-show="node.'
            +d+'.length && node.collapsed" data-ng-click="selectNodeHead(node, $event)"></i><i class="expanded" data-ng-show="node.'
            +d+'.length && !node.collapsed" data-ng-click="selectNodeHead(node, $event)"></i><i class="normal" data-ng-hide="node.'
            +d+'.length"></i> <span data-ng-class="node.selected" data-ng-click="selectNodeLabel(node, $event)">{{node.'
            +h+'}}</span><div data-ng-hide="node.collapsed" data-tree-model="node.'
            +d+'" data-node-id='
            +(c.nodeId||"id")+" data-node-label="
            +h+" data-node-children="
            +d+"></div></li></ul>";
            e&&e.length&&(c.app?(a.$watch(e,function(m,b){
                g.empty().html($compile(k)(a))
            },
            !1),
            a.selectNodeHead=a.selectNodeHead||function(a,b){
                b.stopPropagation&&b.stopPropagation();
                b.preventDefault&&b.preventDefault();
                b.cancelBubble=!0;
                b.returnValue=!1;
                a.collapsed=!a.collapsed
            },
            a.selectNodeLabel=a.selectNodeLabel||function(c,b){
                b.stopPropagation&&b.stopPropagation();
                b.preventDefault&&b.preventDefault();
                b.cancelBubble=!0;
                b.returnValue=!1;
                a.currentNode&&a.currentNode.selected&&(a.currentNode.selected=void 0);
                c.selected="selected";
                a.currentNode=c
            }):g.html($compile(k)(a)))
        }
    }
});

})();

我愿意做的是:当页面加载时,应该会出现一个包含该数据的树,但事实并非如此。在我拆分treeview.js之前,代码是:

(function(){

//angular module
var myApp = angular.module('myApp', ['angularTreeview']);

//test controller
myApp.controller('myController', function($scope){

//test tree model 1
$scope.roleList1 = [
    { "roleName" : "Escapamentos e Catalisadores", "roleId" : "role1", "children" : [

      { "roleName" : "Silencioso Intermediario", "roleId" : "role12", "children" : [
        { "roleName" : "Composicao", "roleId" : "role121", "children" : [
          { "roleName" : "Material 1", "roleId" : "role1211", "children" : [] },
          { "roleName" : "Material 2", "roleId" : "role1212", "children" : [] }
        ]}
      ]}
    ]},

    { "roleName" : "Arquivo 1", "roleId" : "role2", "children" : [] },

    { "roleName" : "Arquivo 2", "roleId" : "role3", "children" : [] }
  ];

//test tree model 2
$scope.roleList2 = [
    { "roleName" : "Tubos", "roleId" : "role1", "children" : [
      { "roleName" : "Galvanizados", "roleId" : "role11", "collapsed" : true, "children" : [] },
      { "roleName" : "Conducao", "roleId" : "role12", "collapsed" : true, "children" : [
        { "roleName" : "Material 1", "roleId" : "role121", "children" : [
          { "roleName" : "Material 2", "roleId" : "role1211", "children" : [] },
          { "roleName" : "Material 3", "roleId" : "role1212", "children" : [] }
        ]}
      ]}
    ]}
  ];

//roleList1 to treeview
$scope.roleList = $scope.roleList;  
});  
})();


(function(l){l.module("angularTreeview",[]).directive("treeModel",function($compile)
{return{restrict:"A",link:function(a,g,c)
{var e=c.treeModel,h=c.nodeLabel||"label",d=c.nodeChildren||"children",
k='<ul><li data-ng-repeat="node in '+e+'">
<i class="collapsed" data-ng-show="node.'+d+'.
length && node.collapsed" data-ng-click="selectNodeHead(node, $event)">
</i><i class="expanded" data-ng-show="node.'+d+'.length && !node.collapsed" 
data-ng-click="selectNodeHead(node, $event)"></i>
<i class="normal" data-ng-hide="node.'+d+'.length"></i>
<span data-ng-class="node.selected" data-ng-click="selectNodeLabel(node, $event)">
{{node.'+h+'}}</span>
<div data-ng-hide="node.collapsed" data-tree-model="node.'+d+'
" data-node-id='+(c.nodeId||"id")+" data-node-label="+h+" data-node-children="+d+">
</div></li></ul>";
e&&e.length&&(c.angularTreeview?(a.$watch(e,function(m,b){
g.empty().html($compile(k)(a))},!1),
a.selectNodeHead=a.selectNodeHead||function(a,b){b.stopPropagation&& 
b.stopPropagation();b.preventDefault&&b.preventDefault();b.cancelBubble=
!0;b.returnValue=!1;
a.collapsed=!a.collapsed},b.selectNodeLabel=a.selectNodeLabel||function(c,b){                                             b.stopPropagation&&b.stopPropagation();
b.preventDefault&&b.preventDefault();b.cancelBubble=!0;b.returnValue=!1;
a.currentNode&&a.currentNode.selected&& (a.currentNode.selected=void0;
c.selected="selected";a.currentNode=c}):g.html($compile(k)(a)))}}})})(angular);

在我拼写该文件之前一切正常(但我只测试了1个模块,换句话说,没有灵活的布局,网格等只有树视图)

我感谢任何提示/建议..

卢卡斯。

1 个答案:

答案 0 :(得分:0)

只应声明一个module来管理整个页面,该模块的名称与ng-app相同。您注入的所有模块都应该在您尝试注入它们之前加载页面...不能注入尚不存在的模块

为主ng-app页面模块注入依赖关系数组中的所有其他依赖模块。

初始化'ng-app`模块后,使用您指定的变量来创建控制器/指令/服务等。

您创建的模块为app,另一个模块为myApp。要同时使用两者,您需要将与ng-app不匹配的那个注入