gpt4 book ai didi

AngularJS:常量与值

转载 作者:行者123 更新时间:2023-12-03 05:56:39 24 4
gpt4 key购买 nike

据我了解文档,常量和值之间的唯一具体区别是常量可以在应用程序配置阶段使用,而值仅在运行阶段可用。

我很好奇为什么在这种情况下需要值?它们真的只是有限的常量吗?

最佳答案

常量可以注入(inject)到任何地方

常量不能被装饰器拦截,这意味着常量的值永远不应该改变

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

app.constant('PI', 3.14159265359);

app.config(function(PI){
var radius = 4;
//PI can be injected here in the config block
var perimeter = 2 * PI * radius;
});

app.controller('appCtrl', function(PI) {
var radius = 4;
// calculate area of the circle
var area = PI * radius * radius;
});

值与常量的不同之处在于,值不能注入(inject)到配置中但可以被装饰器拦截

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

app.value('greeting', 'Hello');

app.config(function ($provide) {
$provide.decorator('greeting', function ($delegate) {
return $delegate + ' World!';
});
});

关于AngularJS:常量与值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30327651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com