Angular.js: .value()는 앱 전체 상수를 설정하고 컨트롤러에서 이를 검색하는 적절한 방법입니까?
안녕하세요, angular.js 비디오를 몇 개 보고 있는데 value() 메서드가 모듈 전체의 상수를 설정하기 위해 사용되고 있는 것을 알았습니다.예를 들어 Angular-UI 라이브러리의 설정을 다음과 같이 설정할 수 있습니다(coffescript).
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']
그러나 앱 모듈에 해당하는 '앱' 변수를 눌러서는 그 값을 얻을 수 없을 것 같습니다.
여기 ST와 angularjs의 구글 그룹에서 읽었는데 공통 코드 btwn 컨트롤러를 공유하는 방법 중 하나는 서비스를 통해서라고 하는데, 이 개념이 여기에도 적용되나요?
감사합니다!
Module.value(key, value)편집 가능한 값을 주입하는 데 사용됩니다.Module.constant(key, value)상수 값을 주입하는 데 사용됩니다.
양자의 차이는 "상수를 편집할 수 없다"는 것이 아니라 $provider로 상수를 가로채 다른 것을 주입할 수 없다는 것입니다.
// 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
});
최근 테스트에서 카르마와 함께 이 기능을 사용하고 싶었습니다.Dan Doyon이 지적한 바와 같이 중요한 것은 컨트롤러, 서비스 등과 같은 값을 주입하는 것입니다..value는 문자열, 개체 배열 등 다양한 유형으로 설정할 수 있습니다.예를 들어 다음과 같습니다.
myvalues.values 값이 포함된 파일 - 카르마 컨피 파일에 값이 포함되어 있는지 확인합니다.
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' ]
언급URL : https://stackoverflow.com/questions/13015523/angular-js-is-value-the-proper-way-to-set-app-wide-constant-and-how-to-retri
'codememo' 카테고리의 다른 글
| "청취자가 true를 반환하여 비동기 응답을 나타냈지만 응답이 수신되기 전에 메시지 채널이 닫혔습니다." 무슨 의미입니까? (0) | 2023.02.12 |
|---|---|
| Oracle에서 여러 행을 콤마 구분 목록으로 결합하려면 어떻게 해야 합니까? (0) | 2023.02.12 |
| Wordpress WooCommerce StoreFront 헤더 스타일 제거 (0) | 2023.02.12 |
| 메이븐 의존성 spring-web vs spring-webmvc (0) | 2023.02.12 |
| 레일: 레일의 POST 422(처리 불가능한 엔티티)경로나 컨트롤러 때문인가요? (0) | 2023.02.12 |