如何在angularjs中创建模块

时间:2014-12-23 05:18:28

标签: javascript angularjs ngroute angularjs-ng-route

在所有的angularjs教程中,我都经历过。模块创建如下

var newApp = angular.module('articles', []); 

或     var routerApp = angular.module('routerApp',['ui.router']);

我用meanjs样板代码开始我的项目,控制器启动如下

angular.module('articles').controller('ArticlesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Articles',
  function($scope, $stateParams, $location, Authentication, Articles) {
    $scope.authentication = Authentication;
    .....
    .....

]);

当我将其更改为

var newApp = angular.module('articles',[]);
newApp.controller('ArticlesController', ['$scope', '$stateParams', '$location', 'Authentication', 'Articles',
  function($scope, $stateParams, $location, Authentication, Articles) {
    $scope.authentication = Authentication;
    .....
    .....

]);

文章的所有路线都停止工作。如果我想在模块中包含一个新组件,我该怎么做。我想将angularFileUpload添加到我的模块中。

angularfileupload中的示例代码是

angular
    .module('app', ['angularFileUpload'])
    .controller('AppController', function($scope, FileUploader) {
        $scope.uploader = new FileUploader();
    });

如果模块已注册,如何添加['angularFileUpload']? 编辑:

articles.client.modules.js

'use strict';

// Use Applicaion configuration module to register a new module
ApplicationConfiguration.registerModule('articles');

2 个答案:

答案 0 :(得分:2)

angular.module("MyModule", [])(带[])是一个setter函数,即 - 它注册一个模块。

没有angular.module("MyModule")

[]是一个getter函数,它会检索以前注册的模块。

两次调用setter重新定义模块。

我不熟悉meanjs样板,但很有可能当你使用setter时,你重新定义了模块,以及之前注册的任何控制器,服务,配置等都被覆盖了。

您需要做的就是更改添加的内容:

var newApp = angular.module("articles");

示例:

angular.module("M", []).controller("A", function(){}); // module M has controller A

angular.module("M").controller("B", function(){}); // module M has controllers A, B

var app = angular.module("M", []); // module M re-registered
app.controller("C", function(){}); // module M has controller C only

答案 1 :(得分:0)

// Create a new module
var myModule = angular.module('myModule', []);

// register a new service
myModule.value('appName', 'MyCoolApp');

// configure existing services inside initialization blocks.
myModule.config(['$locationProvider', function($locationProvider) {
  // Configure existing providers
  $locationProvider.hashPrefix('!');
}]);

用法

angular.module(name, [requires], [configFn]);