Angular.js:.value() 是设置应用范围常量以及如何在控制器中检索它的正确方法

IT技术 javascript angularjs
2021-02-26 12:41:02

嗨,我正在观看几个 angular.js 视频,看到 value() 方法用于设置一种module范围的常量。例如,可以像这样设置 Angular-UI 库的配置:(coffeescript)

angular.module('app',[])
.value "ui.config", 
  tinymce:
    theme: 'simple'
    width: '500'
    height: '300'

我的应用程序目前看起来像这样:

window.app = angular.module("app", [ 'ui'])

.config(["$routeProvider", ($routeProvider) ->
  $routeProvider
  .when "/users",
    templateUrl: "assets/templates/users/index.html"
    controller: IndexUsersCtrl

  .otherwise redirectTo: "/users"

])

.value 'csrf', $('meta[name="csrf-token"]').attr('content') #<---- attention here

IndexUsersCtrl = ($scope) ->
  $scope.users = gon.rabl
  console.log "I want to log the csrf value here" #<---- then attention
IndexUsersCtrl.$inject = ['$scope']

但是我似乎无法通过进入与 app module对应的“app”变量来获得该值。

我在 ST 和 angularjs 的 google group 上读到过,共享通用代码 btwn 控制器的一种方法是通过服务,这个概念也适用于这里吗?

谢谢!

3个回答

Module.value(key, value)用于注入可编辑值, Module.constant(key, value)用于注入常量值

两者之间的区别并不在于您“无法编辑常量”,而在于您无法使用 $provide 截取常量并注入其他内容。

// define a value
app.value('myThing', 'weee');

// define a constant
app.constant('myConst', 'blah');

// use it in a service
app.factory('myService', ['myThing', 'myConst', function(myThing, myConst){
   return {
       whatsMyThing: function() { 
          return myThing; //weee
       },
       getMyConst: function () {
          return myConst; //blah
       }
   };
}]);

// use it in a controller
app.controller('someController', ['$scope', 'myThing', 'myConst', 
    function($scope, myThing, myConst) {
        $scope.foo = myThing; //weee
        $scope.bar = myConst; //blah
    });
'myService' 令牌如何适合图片?
2021-04-21 12:41:02
然而,常量不是一成不变的。你只是不能用另一个注入覆盖它们,因为 $provide 不会拦截它们进行装饰。
2021-05-02 12:41:02
我知道这是一个旧答案,但是“Module.value(key, value) 用于注入可编辑值,Module.constant(key, value) 用于注入常量值”与 ng 不符最新的化身 (1.3.4)。module.value() 和 module.constant() 之间的区别在于:constant() 在应用程序生命周期的早期可用(在配置和运行期间);value() 仅在运行期间可用。它们是否可变,以及更改的值在哪里可见,取决于它们的值结构(原始与否)。docs.angularjs.org/guide/providers#constant-recipe
2021-05-03 12:41:02
@DaveEdelhart,抱歉,我之前没有看到您的问题。我只是将它放在那里作为使用该值的服务的示例。幸运的是,Pavel Hlobil 是一个好心人,他在我的代码中添加了一些注释以使其更加清晰。
2021-05-04 12:41:02
不,它不是“只读”。如果你把一个对象放在那里,任何东西都可能改变那个对象的属性。这主要是因为它是 JavaScript,而不是因为 Angular 有任何特殊的设计问题。但是,我没有看到值以被改变的方式使用,通常我只是看到它用于可注入的“常量”。
2021-05-16 12:41:02

我最近想在测试中将此功能与 Karma 一起使用。正如 Dan Doyon 指出的那样,关键是您可以像控制器、服务等一样注入一个值。您可以将 .value 设置为许多不同的类型 - 字符串、对象数组等。例如:

myvalues.js 一个包含值的文件 - 确保它包含在你的 karma conf 文件中

var myConstantsModule = angular.module('test.models', []);
myConstantModule.value('dataitem', 'thedata');
// or something like this if needed
myConstantModule.value('theitems', [                                                                                                                                                                                                             
  {name: 'Item 1'},                                                                                                                                                                                                                         
  {name: 'Item 2'},                                                                                                                                                                                                                         
  {name: 'Item 3'}
]);                                                                                                                                                                                                                         

]);

test/spec/mytest.js - 也许这是 Karma 加载的 Jasmine 规范文件

describe('my model', function() {
    var theValue;
    var theArray;
    beforeEach(module('test.models'));
    beforeEach(inject(function(dataitem,theitems) {
      // note that dataitem is just available
      // after calling module('test.models')
      theValue = dataitem;
      theArray = theitems;
    });
    it('should do something',function() {
      // now you can use the value in your tests as needed
      console.log("The value is " + theValue);
      console.log("The array is " + theArray);
    });
});

您需要csrf在控制器中引用IndexUsersCtrl = ( $scope, csrf )

IndexUsersCtrl.$inject = [ '$scope', 'csrf' ]