-1

如何将常量的值从一个模块传递到另一个模块?先感谢您。这是Plunker演示。常量“config”在 app.module.js 中定义,我希望能够将它传递给 child.module.js 以定义常量“childConfig”。现在控制台说“配置未定义”。

非常感谢您提前。

// app.module.js
(function(){
    "use strict";

    var myApp = angular
        .module("myApp", ["child"]);

    myApp.constant("config", {
        rootURL: "components/"
        , version: "myApp1.0.0"
    })
})();

// app.component.js
(function(){

    "use strict";

    // Define controller
    function mainController(){
        this.$onInit = function() {
            var mainVM = this;

            mainVM.parent = {
                "lastName": "Smith"
                , "firstName": "Jordan"
            };
        };

    }

    // Define component
    var mainComponent = {
        controller: mainController
        , controllerAs: "mainVM"
    };

    // Register controller and component
    angular.module("myApp")
        .controller("mainController", mainController)
        .component("mainComponent", mainComponent);

})();

//components/child.module.js
(function(){
    "use strict";

    var child = angular.module("child", []);
    
    child.constant("childConfig", config);

})();

//components/child.component.js
(function(){
    "use strict";

    // Define controller
    function childController(config) {
        this.$onInit = function() {
            var vm = this;
            
            vm.getTemplateUrl = function(){
              return config.rootURL + 'child.html';
            }
            vm.rootURL = config.rootURL;
            vm.version = config.version;
            vm.child = {
              "firstName": "Jack"
            }
        };
        // end of $onInit()
    }

    // Define component
    var child = {
        //templateUrl: vm.getTemplateUrl + "child.html"
        //templateUrl: "components/child.html"
        template: "<ng-include src='vm.getTemplateUrl()'/>"
        , controller: childController
        , controllerAs: "vm"
        , bindings: {
            parent: "<"
        }
    };


    // Register controller and component
    angular.module("child")
        .controller("childController", childController)
        .component("child", child);

})();
<!DOCTYPE html>
<html>

  <head>
    <script src="//code.angularjs.org/snapshot/angular.js"></script>
    <link rel="stylesheet" href="style.css">
    <script src="app.module.js"></script>
    <script src="app.component.js"></script>
    <script src="components/child.module.js"></script>
    <script src="components/child.component.js"></script>
  </head>

  <body ng-app="myApp" ng-controller="mainController as mainVM">
    Parent: {{mainVM.parent.firstName}} {{mainVM.parent.lastName}}<br>
    <child parent="mainVM.parent"></child>
  </body>

</html>

4

1 回答 1

2

我想你可能误解了constant工厂的能力。它不应该是可变的,也不是变量,因此它不能使用另一个外部提供者来组成它的值,它只会提供一个纯的纯值。

另一方面,如果您不介意,可以使用工厂来实现您正在寻找的结果。基本上,您可以创建一个工厂,根据注入的config提供者为您制作一个字符串。

例如:

child.factory("childConfigURL", ['config', function(config) {
    return config.rootURL + "/component/";
}]);

这种方法的唯一问题是它不能被注入到模块配置函数中,因为它不再是纯粹的,需要引导来解决它的依赖关系。

常量注意事项:

constant(name, value);

使用 $injector 注册一个常量服务,例如字符串、数字、数组、对象或函数。像值一样,不可能将其他服务注入常量。

但与 value 不同的是,常量可以注入到模块配置函数中(参见 angular.Module),并且不能被 AngularJS 装饰器覆盖。

参考:$provide.constant

于 2017-09-19T20:09:55.633 回答