2016-09-22 2 views
0

작동에 대한 정보가 필요한 지시문을 작성 중입니다. 그러나 그 정보는 약속에서 나옵니다. 지시문 자체가 더 많은 시간을 요구하기 때문에 그 정보없이 지시문이로드됩니다.지시어 링크 실행 전 각도 데이터를 얻으십시오.

angular.module('app').directive('tagInput', tagInput); 

var tagInput = function ($timeout, profileService) { 

    var profileList = profileService.getProfileList(); 

    profileList.then(function (profiles) { 
    var profileObject = {}; 
    var profilesFiltered = []; 

    for(var index in profiles){ 
     if(profiles[index].hasOwnProperty('id')){ 
     var notStandard = profiles[index].id.match(/\d+/g); 
     if (notStandard != null) { 
      profileObject.icon = '<i class="fa fa-bookmark" aria-hidden="true"></i>'; 
     } 
     else{ 
      profileObject.icon = '<i class="fa fa-user" aria-hidden="true"></i>'; 
     } 
     profileObject.name = profiles[index].name; 
     profileObject.identifier = profiles[index].id; 
     profileObject.ticked = false; 
     profilesFiltered.push(profileObject); 
     } 
    } 

    return profilesFiltered; 
    }); 

return { 
    restrict: 'EA', 
    require: 'ngModel', 
    scope: { 
    tags: '=ngModel', 
    searchParams: '=searchParams' 
    }, 
    replace: false, 
    link: function (scope, element, attrs) { 

    scope.modelFilter = { 
     showSearchPanel: false, 
     inputProfiles: profilesFiltered, 

... 

이렇게하면 항상 템플릿의 데이터가 비어있게됩니다. 지시어 링크 실행 전에 어떻게 데이터를 준비 할 수 있습니까? 아마도 inputAccounts: accountsFiltered,과 비슷한 약속이 필요합니다.

답변

0

처음에는 지시어 렌더링과 약속 간의 종속성을 수행 할 수 없지만 방법은 있지만 반 패턴입니다. 다른 관점이 필요합니다.

예를 들어 약속이 끝날 때만 scope.modelFilter이 채워지기를 원한다면 지시어의 링크 기능에서 감시자를 만들 수 있습니다. 필터링 된 필드와 콜백 내부에서 modelFilter을 채워보세요.