AngularJS: Constants vs Values

前端 未结 3 1436
离开以前
离开以前 2020-12-04 11:43

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

3条回答
  •  [愿得一人]
    2020-12-04 12:07

    The difference between value and constant is that a value specified using constant is available during the configuration phase.

    Well it’s the same for value and constant. constant is available from the configuration phase and value is not.

    The other difference is as the name implies you can’t change the value of a constant. The first value you assign it is the value it keeps, if you try to assign it a different value later it will be ignored.

    Here’s an example:

    mod.value("myValue", "First Assignment");
    
    mod.value("myValue", "Second  Assignment");
    
    mod.constant("myConstant", "First Assignment");
    
    mod.constant("myConstant", "Second Assignment");
    
    mod.controller("MyController", function(myValue, myConstant) {
    
        console.log("myValue: " + myValue);
    
        console.log("myConstant: " + myConstant);
    });
    

    Console output:

    myValue: Second Assignment
    
    myConstant: First Assignment
    

提交回复
热议问题