배경 : 연락처 목록을 collection-repeat
(Angular의 ng-repeat
이상의 이오니아 개선)을 사용하여 표시하는 이안 응용 프로그램이 있습니다. 맨 위에 표시 할 연락처 목록을 결정하기위한 두 개의 토글 버튼이 있습니다. 연락처의 그림, 제목 및 부제목을 표시하는 사용자 지정 문을 작성했습니다.사용자 지정 지시문의 범위 모델이 정의되지 않았습니다.
HTML
<ion-content>
<ion-list>
<div class="item item-avatar" collection-repeat="contact in viewing" force-refresh-images="true">
<profile-or-initials ng-model="contact"></profile-or-initials>
<h2>{{contact.title}}</h2>
<p>{{contact.subtitle}}</p>
</div>
</ion-list>
</ion-content>
JS
var app = angular.module('myApp', ['ionic']);
app.directive('profileOrInitials', function($state) {
return {
restrict: "E",
scope: {
ngModel: '='
},
replace: true,
template: '<div class="profileOrInitials"></div>',
link: function(scope, element) {
var getInitials = function(user) {
return user.title.substring(0, 2);
};
var process = function() {
var image = scope.ngModel.feed_pic_url;
var initials = getInitials(scope.ngModel);
var html = '';
if (image) {
html = '<img src="' + image + '"></img>';
} else {
image = "http://science-all.com/images/wallpapers/blue-image/blue-image-14.jpg";
html = '<img src="' + image + '"></img><div class="initials">' + initials + '</div>';
}
element.html(html);
};
//var destroy = scope.$watch('ngModel', function() {
// if (scope.ngModel) {
process();
//destroy();
//}
//});
}
};
})
문제 : 나는 그것을 위해 scope.$watch
기능을 추가하지 않는 한이 지침의 ngModel
범위는 undefined
입니다. 심지어 시계에서 destroy
함수를 호출하면 단추를 클릭하면 그림이 업데이트되지 않기 때문에 지시문도 작동하지 않습니다.
여기에서 플 런커 : http://plnkr.co/edit/t4opQdqr4VStpqP6MdQn.
누구에게 설명 할 수 있습니까? 나는 실제 해결책에 관심이 없다. 나는 무슨 일이 일어나고 있는지 이해하지 못한다. 내 의구심은 내 지시문의 link
함수가 collection-repeat
전에 실행 중이므로 그 시점에서 모델이 정의되지 않았지만 우선 순위를 낮추려고 시도했지만 여전히 작동하지 않습니다. (collection-repeat has a priority of 1000).
나에게이과에 allways 투쟁을 사용하여 검색 할 수 있습니다. 시계 솔루션이 나를 위해 작동합니다. – user3791775
상단의 버튼 : "google contacts"및 "facebook contacts":) –
또한 ngModel 컨트롤러 기능이 필요없는 항목에 대해 'ngModel'을 사용하는 지점이 없습니다. 그냥'$ watch'의'newValue' 인수가 정의되어 있고 잘 작동해야합니다. – charlietfl