角度:如何从配置到主模块按照约翰爸爸

问题描述:

我想设置的服务的“基本URL”我的角度应用程序,这样我可以在我的项目中使用相对URL建议角的最佳实践传递价值。角度:如何从配置到主模块按照约翰爸爸

我发现初始化,可以存储一些基本的配置数据和该模块的配置模块的概念,以后可以注入到主模块作为dependency.The解决方案是here

但我也想跟着约翰爸爸的angular best practices

不知怎的,我不能合并这两个理念,生产的结果。

含有我的基本控制器CONFIGS

angular.module('app.config', []) 
    .value('app.config', { 
    basePath: 'my-base-url' // Set your base path here 
}); 

我的主应用程序模块和控制器

angular.module('userManage',['ngMaterial', 'ngPassword', 'app.config']) 
.controller('addUserController', addUserController); 


function addUserController($scope, $http, $window){ 
    $scope.pattern1 = /^[a-zA-Z0-9]*$/; 
    $scope.submitForm = function(){ 
    $http({ 
     method : 'POST', 
     url : 'my-relative-url',//(basePath+'relative url') 
     data : $scope.user, 
    }); 
}; 

}

在教程的第二个参数被作为数组传递和值作为传递函数参数,如何实现这在约翰爸爸的推荐方式

还有像一个确定的基URL值作为常数等,但一个确定的基模块,用于存储所有配置其他的解决办法似乎整洁和方便。

value名称将导致你的麻烦。坚持使有效的JavaScript变量名称。

我也建议采用恒定

angular.module('app.config', []) 
    .constant('BASE_PATH', 'my-base-url'); 

angular.module('userManage',['ngMaterial', 'ngPassword', 'app.config']) 
    .controller('addUserController', addUserController); 


function addUserController($scope, $http, $window, BASE_PATH) { 
    // now you can use BASE_PATH 

总结,包括app.config模块作为依赖使得其常数,价值观,工厂,服务和其他供应商可供注射。

+0

这做了trick.But为什么使用而不是价值不变?多谢 – jayadevkv

+1

@jayadevkv值可以被其他模块改变,常量可能不会。此外,常量可以在模块'config'块中使用,这可能会有所帮助。 – Phil