加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – angular.js指令在不同的模块

发布时间:2020-12-17 07:42:19 所属栏目:安全 来源:网络整理
导读:我很困惑.如果我每次只能使用ng-app一次,那么如何使用不同模块中的指令,不同的.js文件.看: script src='mainModule.js'/script src='secondModule.js'/body ng-app='mainModule' my-thingy/ div other-thingy/ /div /bodymainModule.js: angular.module("ma
我很困惑.如果我每次只能使用ng-app一次,那么如何使用不同模块中的指令,不同的.js文件.看:
<script src='mainModule.js'/>
<script src='secondModule.js'/>
<body ng-app='mainModule'>
   <my-thingy/>
   <div>
      <other-thingy/>
   </div> 
</body>

mainModule.js:
   angular.module("mainModule",[]).directive("myThingy",function(){ ... })

secondModule.js:
   angular.module("secondModule",[]).directive("otherThingy",function(){ ... })

那么,我现在如何指向页面上的第二个模块,而不是从mainModule.js引用它

有一个解决方案可以通过编程方式在页面上引导几个角度模块( docs).

您必须从html中删除ng-app属性.

定义几个模块:

var app1 = angular.module('myApp1',[]);

var app2 = angular.module('myApp2',[]);

app1.controller('MainCtrl',function($scope) {
  $scope.name = 'App1';
});

app2.controller('MainCtrl',function ($scope) {
  $scope.name = 'App2';
})

然后在index.html中做:

<html>
  <head></head>
  <body>
    <!-- Module 1 -->
    <div id="myApp1">
      <div ng-controller="MainCtrl">
        <h1>Hello {{name}}</h1>
      </div>
    </div>
    <!-- Module 2 -->
    <div id="myApp2">
      <div ng-controller="MainCtrl">
        <h1>Hello {{name}}</h1>
      </div>
    </div>
    <!-- Bootstrap modules -->
    <script>
      angular.element(document).ready(function() {
          angular.bootstrap(document.getElementById('myApp1'),['myApp1']);
          angular.bootstrap(document.getElementById('myApp2'),['myApp2']);
     });
    </script>
  </body>
  </html>

这是一个工作plnkr与这个解决方案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读