지향성 :
app.directive('sample', function() {
return {
restrict: 'A',
scope: {
'sample' : '=',
'another' : '='
},
link: function (scope, element, attrs) {
console.log(attrs);
scope.$watch('sample', function (newVal) {
console.log('sample', newVal);
});
scope.$watch('another', function (newVal) {
console.log('another', newVal);
});
}
};
});
이 쓰인다.
틀 :
<div example-directive example-number="99" example-function="exampleCallback()"></div>
지침 :
app.directive('exampleDirective ', function() {
return {
restrict: 'A', // 'A' is the default, so you could remove this line
scope: {
callback : '&exampleFunction',
},
link: function (scope, element, attrs) {
var num = scope.$eval(attrs.exampleNumber);
console.log('number=',num);
scope.callback(); // calls exampleCallback()
}
};
});
fiddle
속성 example-number
의 값이 하드 코딩 될 경우, 한번 $eval
을 사용하고, 기억 제안 값. 변수 num
은 올바른 유형 (숫자)을 갖습니다.
지시어가 생성하는 범위 유형 (있는 경우)에 따라 다릅니다. 선택 범위는 다음과 같습니다 : 새로운 스코프가 없거나 (기본값, 또는 'scope : false'로 명시 적으로), 새로운 스코프 (일반적인 프로토 타입 상속, 즉 scope : true)로 범위를 분리하십시오 (예 : scope : {...}'). 지침의 범위는 어떤 유형입니까? –
@MarkRajcok 분리 범위가 있습니다. – Undistraction