繁体   English   中英

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

[英]Angular.js: Is .value() the proper way to set app wide constant and how to retrieve it in a controller

您好,我正在观看几个angular.js视频,并看到value()方法用于设置一种模块范围的常量。 例如,可以像这样设置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模块相对应的'app'变量来获得该值。

我在ST上阅读了这篇关于angularjs谷歌小组的消息,通过服务分享公共代码btwn控制器的一种方法是,这个概念也适用于此吗?

谢谢!

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
    });

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

myvalues.js包含值的文件 - 确保它包含在您的业力配置文件中

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' ]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM