角度 - 使用常量配方与价值配方

Lui*_*lli 0 angularjs

后,我看了这里我得到了好奇的东西:

我为什么要用value而不是constant?因为常量可以作为值使用,也可以在config阶段中使用

ave*_*Joe 5

  • Value 可以在运行时更改,即装饰.
  • Constant 是固定的 - 不能改变.
angular.module('colors', [])
       .value('favoriteColors', ['red']);

angular.module('main', ['colors']);
       .config(function config($provide) {
           $provide.decorator('favoriteColors', function($delegate) {
               return $delegate.concat(['green']);
           });
       })
       .controller('MainCtrl', function MainController(favoriteColors) {
           console.log('Favorite colors are:', favoriteColors); // red, green
       });
Run Code Online (Sandbox Code Playgroud)