Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在运行时更新angularjs常量?_Javascript_Angularjs_Constants - Fatal编程技术网

Javascript 如何在运行时更新angularjs常量?

Javascript 如何在运行时更新angularjs常量?,javascript,angularjs,constants,Javascript,Angularjs,Constants,我在应用程序中定义了常数,如下所示: angular.module('configuration', []) .constant('Engine_API', 'http://sample.io:1929/') var testApp = angular.module('Duot', [ 'configuration'] testApp .controller('userProfileCtrl', function ($scope,Engine_API) { } 我在应用程序中使用如下 an

我在应用程序中定义了常数,如下所示:

angular.module('configuration', [])
 .constant('Engine_API', 'http://sample.io:1929/')
var testApp = angular.module('Duot', [ 'configuration']
testApp .controller('userProfileCtrl', function ($scope,Engine_API) {
}
我在应用程序中使用如下

angular.module('configuration', [])
 .constant('Engine_API', 'http://sample.io:1929/')
var testApp = angular.module('Duot', [ 'configuration']
testApp .controller('userProfileCtrl', function ($scope,Engine_API) {
}

有没有办法在运行时修改并将新值设置为常量?

您不能更改角度常量值。但若要更改值,可以使用角度值提供程序而不是常量

angular.module('configuration', [])
 .value('Engine_API', 'http://sample.io:1929/')

根据定义,假定它是常量,则不能。如果您正在寻找易于更新的中央存储,请考虑使用<代码>值< /代码>。code>.value('myData',{Engine\u API:'http://sample.io:1929/' });然后,您可以根据需要访问和更新
myData.Engine\u API
。为什么不创建一个数据服务来共享一些常规数据?常数非常适合于从不更改的值,或者使用可以在运行时设置的提供程序