데이터 목록에서 값을 조회하고 항상 해당 값을 표시하는 지시문을 작성하고 있습니다. 이 plunkr 예제에서 데이터 변경 버튼을 클릭해도 결과가 변경되지 않는 이유는 무엇입니까? $ watch로 시도했지만 한 번만 실행됩니다.
플렁크 : http://plnkr.co/edit/9A3z0KMm8EaBNgYv1NmH?p=preview
예를 들어, 다음은 사용법입니다.
<lookup-binder lookup="lookupList" find="data.id" result="lookupList.description"></lookup-binder>
지시문은 다음과 같습니다.
app.directive('lookupBinder', function() {
return {
restrict: 'E',
scope: {
lookup: '=',
find: '=',
result: '='
},
controller: function($scope){
console.log('controller fired');
},
link: function(scope, element, attrs, tabsCtrl) {
console.log('link fired')
scope.$watch(scope.find, function(newValue, oldValue) {
console.log('watch fired');
var el = _.find(scope.lookup, { id: scope.find });
if(el != null){
scope.result = el.description;
}
});
},
template: 'lookup Binder: <br/> lookupList: {{lookup}} <br/> find: {{find}} <br/> result: {{result}} <br/>'
};
});
제어 장치:
var appName = angular.module('app', []);
angular.module('app').controller('testController', TestController);
TestController.$inject = ['$scope'];
function TestController($scope) {
$scope.gradeLevels = [{
id: '1',
description: 'kindergarden'
}, {
id: '2',
description: 'first grade'
}];
$scope.gradeLevel = {
gradeLevelID: '1',
record: 'test1'
};
console.info('gradeLevels[0].id = ' + $scope.gradeLevels[0].id);
console.info('gradeLevel.gradeLevelID = ' + $scope.gradeLevel.gradeLevelID);
}
범위의 $watch
함수는 문자열 또는 함수를 취하므로 다음 중 하나로 변경하십시오.
scope.$watch('find', function(newValue, oldValue) {
...
}
또한의 결과 _.random
는 숫자이고 ID는 문자열이므로 임의를 문자열로 변경하십시오.
$scope.data.id = _.random(1,4).toString();
이 플 런커 확인
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다