2017-04-15 8 views
0

나는 각도 uib-aheadahead를 사용하는 앱을 가지고 있습니다.uib-typahead를 사용한 AngularJs 필터링이 작동하지 않습니다.

데이터는 ajax 호출로 원격으로로드됩니다.

"name"이 $ viewValue 문자열을 포함하는 결과 만 표시하려면 결과를 필터링해야합니다.

여기 내 코드입니다. 내 문제는 데이터가 결코 필터링되지 않는다는 것입니다.

내가 뭘 잘못하고 있니?

는 마크 업

<input type="text" ng-model="modelo.tuss" placeholder="Select TUSS" 
    uib-typeahead="item as item.name for item 
    in getTabelaTUSS($viewValue) | filter:{name:$viewValue}" 
class="form-control"> 

는 // 컨트롤러

angular.module("clinang").controller('exameCtrl',['$scope', function($scope) { 
     var prof=[{"id":1,"name":"John Prof"}, 
     {"id":2,"name":"Mary Prof"}]; 

     $scope.getTabelaTUSS = function(val) { 
     return dataService.getTabelaTUSS().then(function(response){ 
      return prof; //only to simulate results to test 
    }); 
    }; 

}]); 

이 업데이트 컨트롤러 // : 당신이 return prof;과 다음 블록을 교체하는 경우

//first option - geting rid off view filter and making local filter in controller 
angular.module("clinang").controller('configAgendaAddProcedimentosCtrl',['$scope','dataService','$state','$filter',function($scope,dataService,$state,filter){ 
     $scope.getTabelaTUSS = function(val) { 
     return dataService.getTabelaTUSS().then(function(response){ 
       return filterFilter(response.data, val); 
     }); 
     }; 
}]); 

//second option - using view filter and no local filter in controller 
//I also tried without success 
angular.module("clinang").controller('configAgendaAddProcedimentosCtrl',['$scope','dataService','$state','$filter',function($scope,dataService,$state,filter){ 
     $scope.getTabelaTUSS = function(val) { 
     return dataService.getTabelaTUSS().then(function(response){ 
       return response.data; 
     }); 
     }; 
}]); 

답변

0

, 그것은 작동합니다 예상대로 내가 보여준대로당신의 return 문 (210)

return dataService.getTabelaTUSS().then(function(response){ 
    return prof; //only to simulate results to test 
}); 

문제

dataService.getTabelaTUSS().then() 호출에 의해 반환되는 약속 개체를 반환합니다. 이 문제를 해결하려면 profthen 내에 반환하는 대신 prof을 범위 변수에 할당하고보기에서 사용하십시오.

+0

감사합니다. 나는 더 잘 설명 할 것이다. 내가 코멘트 코드에서 말했듯이 나는 단지 prof를 사용하여 테스트에서 "response.data"가 실제로 필요하고 뷰 또는 컨트롤러에 "$ viewValue"를 사용하여 필터 "응답"이 필요합니다. –

+0

@LuizAlves 나는 이해하고 나의 제안을 적용 할 수도있다. scope variable에'response.data'를 할당하고 뷰에서 사용하십시오. – Hoa

+0

안녕하세요. 작동하지 않습니다. 나는 다음과 같이 컨트롤러에 필터를 시도했다. $ scope.getTabelaTUSS = function (val) { return dataService.getTabelaTUSS(). (function (response) { return filterFilter (response.data, val); }); }; 또는 "return response.data;"를 사용하여보기 그러나 아무것도 작동하지 않습니다. –

0

dataService.getTabelaTUSS() 메서드를 호출하고 있지만 컨트롤러에 dataService을 주입하지 않았습니다.

다음과 같이 컨트롤러에 dataService을 삽입하십시오.

angular.module("clinang").controller('exameCtrl',['$scope','dataService' function($scope,dataService) { 
    var prof=[{"id":1,"name":"John Prof"}, 
    {"id":2,"name":"Mary Prof"}]; 

    $scope.getTabelaTUSS = function(val) { 
     return dataService.getTabelaTUSS().then(function(response){ 
      return prof; //only to simulate results to test 
    }); 
    }; 

}]); 

참고 : HTML 코드에서 dataService 서비스 파일을 포함하는 것을 잊지 마십시오.

또한 dataService.getTabelaTUSS() 메서드 response 대신 prof 개체를 사용하는 코드가 표시됩니다. 따라서 서비스의 데이터가 필요하지 않은 경우 다음과 같이 메소드를 업데이트하십시오.

$scope.getTabelaTUSS = function(val) { 
    return prof; 
} 
+0

답변 해 주셔서 감사합니다. 나는 dataService를 사용한다. 단순함을 위해 나는 그것을 생략했다. 위에서 말했듯이 나는 promise에서 "response.data"가 실제로 필요하며 view 또는 controller에 "$ viewValue"를 사용하여 필터 "응답"이 필요하다. –

+0

"응답"을 필터링하려면 위에서 언급 한대로 dataService를 주입하십시오. –