Angular.js : .value ()는 앱 전체 상수를 설정하는 적절한 방법이며 컨트롤러에서 검색하는 방법입니다.
안녕하세요 저는 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'변수를 탭하여 그 값을 얻을 수없는 것 같습니다.
나는 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 값을 포함하는 파일-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' ]
'program tip' 카테고리의 다른 글
iFrame이로드 될 때로드 메시지를 표시하는 방법은 무엇입니까? (0) | 2020.09.13 |
---|---|
PHP의 mysql 테이블에서 count (*)를 선택하십시오. (0) | 2020.09.13 |
LESS에 "확장"기능이 있습니까? (0) | 2020.09.12 |
JaCoCo 실행시 "실행 데이터 파일 누락으로 인해 JaCoCo 실행 건너 뛰기"받기 (0) | 2020.09.12 |
std :: array의 기본 초기화? (0) | 2020.09.12 |