AngularJS:常量VS值 [英] AngularJS: Constants vs Values

查看:109
本文介绍了AngularJS:常量VS值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As far as I understand the documentation, the only concrete difference between a Constant and a Value is that a Constant can be used during the apps config phase, whereas a Value is only available during the run phase.

我很好奇,为什么在所有需要在这种情况下值?他们不是真的只是常量限制?

I am curious as to why Values are needed at all in this case? Aren't they really just limited Constants?

推荐答案

常量可以的随时随地注入

一个常量不能被一个装饰截获,这意味着,一个恒定的值不应该改变

A constant can not be intercepted by a decorator, that means that the value of a constant should never be changed.

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

app.constant('PI', 3.14159265359);

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

从价值不断的区别在于值不能被注入到配置 但它可以通过装饰截获

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

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

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

这篇关于AngularJS:常量VS值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆