컨트롤러 기능에서 지시문의 속성에 액세스하려고합니다. 그러나, 내가 액세스 할 때까지 그것은 정의되지 않습니다. 나는 그것이 간단한 타이머를하면 작동하는 것으로 나타났습니다. 지시어와 스코프가 준비되고 사용되도록 설정된 후에 만 코드를 실행하는 방법이 있습니까?AngularJS 지시문은 컨트롤러에서 액세스를 특성화합니다.
나는 그것으로 바이올린을 만들었습니다. 콘솔이 열려 있는지 확인하십시오. 여기
http://jsfiddle.net/paulocoelho/uKA2L/1/ 내가 바이올린에 사용하고있는 코드입니다 :
<div ng-app="testApp" >
<testcomponent text="hello!"></testcomponent>
</div>
당신은 연결과 제어 기능 내부
scope.text = $attrs.text;
을 설정 한 경우입니다 작동 무엇
var module = angular.module('testApp', [])
.directive('testcomponent', function() {
return {
restrict: 'E',
template: '<div><p>{{text}} This will run fine! </p></div>',
scope: {
text: '@text'
},
controller: function ($scope, $element) {
console.log($scope.text); // this will return undefined
setTimeout(function() {
console.log($scope.text); // this will return the actual value...
}, 1000);
},
link: function ($scope, $element, $attrs) {
console.log($scope.text);
setTimeout(function() {
console.log($scope.text);
}, 1000);
}
};
});
응을, 나를 위해 작동합니다 :) 덕분에 남자 – PCoelho
이 linting을 실패합니다. 'attrs.text'는 알려지지 않았기 때문에. –
@DougChamberlain JSDoc을 사용하여이 문제를 해결할 수 있습니다. ''@param {string} attrs.text''와 같은 것이 여러분의 문제를 해결할 것입니다. –