2017-02-21 47 views
0

我想設置的服務的「基本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值作爲常數等,但一個確定的基模塊,用於存儲所有配置其他的解決辦法似乎整潔和方便。

回答

1

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