AngularJS 상수
AngularJS를 사용하여 다른 상수에 상수를 주입할 수 있습니까?
예.
var app = angular.module('myApp');
app.constant('foo', { message: "Hello" } );
app.constant('bar', ['foo', function(foo) {
return {
message: foo.message + ' World!'
}
}]);
설정 루틴에 각도 상수를 주입해야 하기 때문에 각도 상수를 사용해야 합니다.
app.config(['bar', function(bar) {
console.log(bar.message);
}]);
상수와 프로바이더를 설정 루틴에만 주입할 수 있다는 것을 알고 있습니다.또한 프로바이더에 의존성 주입을 실행할 수 있다는 것도 알고 있습니다만, 이러한 시나리오에서는 최적의 방법이 아닌 것 같습니다.
잘 부탁드립니다!
맞습니다. foo와 bar를 모두 상수로 등록하는 것은 불가능합니다.
또한 프로바이더를 회피책으로 사용하는 경우 데이터를 프로바이더인스턴스에 저장해야 한다는 점을 제외하고 거의 올바르게 처리했습니다.
var app = angular.module('myApp', []);
app.constant('foo', {
message: 'Hello'
});
app.provider('bar', ['foo', function(foo) {
this.data = {
message: foo.message + ' World!'
};
this.$get = function() {
return this.data;
};
}]);
다음으로 config block에 바의 공급자인스턴스를 삽입합니다(바인스턴스는 설정 단계에서는 아직 사용할 수 없기 때문에 바인스턴스가 아닙니다).
app.config(['barProvider', function(barProvider) {
console.log(barProvider.data.message);
}]);
이게 도움이 됐으면 좋겠다.
또 다른 접근법은 즉시 호출된 함수식을 사용하여 함수를 정의하고 즉시 호출하는 것입니다.이렇게 하면 식이 두 개의 상수로 구성될 수 있는 함수의 값을 평가하고 반환합니다.
이것은 다음과 같은 방법으로 달성할 수 있습니다.
app.constant("foo", (function() {
var message = 'Hello'
return {
foo: message,
bar: message + " World!"
}
})());
그런 다음 다음과 같은 상수를 사용합니다.
console.log("foo: " + foo.foo);
console.log("bar: " + foo.bar);
이 방법에 접근하는 가장 좋은 방법은 두 번째 상수를 공급자로 만드는 것입니다.
var app = angular.module('myApp');
app.constant('foo', { message: "Hello" } );
app.provider('bar', ['foo', function(foo) {
this.$get = function() {
return {
message: foo.message + ' World!'
};
}
}]);
그 후:
app.config(['bar', function(bar) {
console.log(bar.message);
}]);
myApp.constant('bar', {
get message() {
Object.defineProperty(this, 'message', {
value: angular.injector(['myApp']).get('foo').message,
writable: false,
enumerable: true,
configurable: true
});
return this.message;
}
})
이렇게 느린 로드를 사용하려고 합니다. 먼저 값을 설정하십시오.get
한 상수를 다른 상수에 주입할 수 없습니다.각도로는 안 돼요.하지만 아래와 같은 서비스를 통해 달성할 수 있습니다.
angular.module('myApp', [])
.constant('firstName','John')
.constant('lastName','Smith')
.service('name',function(firstName,lastName) {
this.fullName = firstName + ' ' + lastName;
})
.controller('mainCtrl',function($scope,name) {
$scope.name = name.fullName;
});
또한 addon에서는 향후 상수값이 변경될 경우 상수만 사용할 수 있으므로 상수를 사용하는 것은 의미가 없습니다(단, Javascript는 상수를 완전히 지원하지 않습니다).글로벌 변수를 사용하지 않는 angularjs의 또 다른 기능인 상수 대신 값을 사용할 수 있습니다.
종속성 구문을 지원하지 않는 것처럼 보입니다. 값이 개체일 수 있는 키 값 쌍일 뿐입니다.
다음은 상수를 만들고 사용하는 간단한 방법입니다.
var app=var.param #app_name#,[];Open the browser in chrome and see the result in console of the browser.
//상수를 정의합니다.
app.constant("constant Service", {attr:"이것이 최초의 content"});
app.controller("#controller_name#), app.controller($scope, constantService)}{
Service console.log(constant Service);
console.log(constService.attr);
Thanking you.
언급URL : https://stackoverflow.com/questions/24831323/angularjs-constants
'programing' 카테고리의 다른 글
Oracle sql에서 "%Type"은 무엇을 의미합니까? (0) | 2023.02.17 |
---|---|
전개 후 "Uncatched Error: [$injector:unpr]"가 각진 상태로 표시됩니다. (0) | 2023.02.17 |
javascript에서 문자열 배열을 배열로 변환 (0) | 2023.02.17 |
wordpress로 스크립트를 큐잉할 때 get_stylesheet_directory_uri()와 get_template_directory_uri()의 차이점은 무엇입니까? (0) | 2023.02.17 |
Oracle에서의 뷰란? (0) | 2023.02.17 |