Modules and namespace / name collision in AngularJS

前端 未结 4 1968
故里飘歌
故里飘歌 2020-11-30 05:41

Consider the following jfiddle http://jsfiddle.net/bchapman26/9uUBU/29/

//angular.js example for factory vs service
var app = angular.module(\'myApp\', [\'mo         


        
4条回答
  •  感动是毒
    2020-11-30 06:02

    Unfortunately, there is no namespacing in AngularJS. One solution is to use a prefix (another solution may be this!). See the following example:

    // root app
    const rootApp = angular.module('root-app', ['app1', 'app2']);
    
    // app 1
    const app1 = angular.module('app1', []);
    app1.controller('app1.main', function($scope) {
      $scope.msg = 'App1';
    });
    
    // app2
    const app2 = angular.module('app2', []);
    app1.controller('app2.main', function($scope) {
      $scope.msg = 'App2';
    })
    
    
    
    
    
    {{msg}}
    {{msg}}

提交回复
热议问题