나는 $ angle을 정확하게 사용하는 방법을 고민하고 있습니다. 내 응용 프로그램은 기본 페이지를로드 할 때 서버에서받은 json 데이터의 URL 링크를 기반으로보기를 업데이트합니다.
컨트롤러는 버튼을 클릭 할 때 다음 데이터 파일을 가져 오지만이 데이터를 내 범위 변수에 삽입하고 뷰를 업데이트 할 수 없습니다. 네트워크가 데이터를 성공적으로 전송하므로 내 서비스가 작동합니다.
$ scope를 $ resource promise를 해결하고 데이터를 검색 할 때 앱이 초기화 된 후에 뷰를 업데이트하는 방법을 이해할 수 없습니다. 이 문제에 대한 도움을 주시면 감사하겠습니다.
내 ListController :
//View updates correctly when promise resolves
$scope.data = DataElements.get({});
$scope.nextPage = function(){ //Called when clicking 'next' button
var url = $scope.data.pager.nextPage;
var pageNumber = url.split("page=").pop(); //Ugly but it is temporary, I promise..
//View doesnt update
DataElements.get({'page': pageNumber}, function(data){
$scope.data = data;
});
//Error: Digest already in progress
DataElements.get({'page': pageNumber }, function(data){
$scope.$apply(function(){
$scope.data = data;
});
});
//View doesnt update
$scope.data = DataElements.get({'page': pageNumber)};
};
서비스 :
services.factory('DataElements', ['$rootScope','$resource', function($rootScope, $resource){
return $resource($rootScope.baseUrl+'api/:endPointAdr.jsonp',
{'endPointAdr': 'dataElements', 'page': '@page'},
{ get : {'method' : 'JSONP', 'params' : {'callback' : 'JSON_CALLBACK'}}
});
}]);
보기 :
<section>
<div>
<h1>Search</h1>
<input type="text" placeholder="Type something" autofocus>
<input type="button" name="filter" value="Filter">
<input type="button" name="clear" value="Clear">
<div class="btn-toolbar" role="toolbar" aria-label="...">
<a class="btn btn-default" href="#" aria-label="Previous Page">
<span class="glyphicon glyphicon-chevron-left"></span>
</a>
<a class="btn btn-default" href="#" ng-click="nextPage()" aria-label="Next Page">
<span class="glyphicon glyphicon-chevron-right"></span>
</a>
</div>
</div>
<div ng-controller="ListController">
<ul class="" ng-show="data">
<li class="" ng-repeat="element in data.dataElements ">
<a href="#">
<div class="info">
<h2>{{element.name}}</h2>
</div>
</a>
</li>
</ul>
</div>
</section>
서버 응답 :
DataElements.get({}) == >
angular.callbacks._1({"pager": {"page":1,"pageCount":15,"total":706,"nextPage":"http// ...});(*)
DataElements.get({'page': 2}) == >
angular.callbacks._2({"pager": {"page":2,"pageCount":15,"total":706,"nextPage":"http:// ...});(*)
(*) $ 자원은 JSONP 스트립 만 JSON을 반환합니다.
업데이트 :
나는 단지 그것을 완벽하게 작동 nextPage
기능 범위에 $scope.data
을 지정합니다. ng-click
지시문이 $scope.apply()
에이 함수를 래핑하는 것을 알고 있습니다. 그런 다음 apply를 명시 적으로 호출하면 오류가 발생 함을 의미합니다.
해결책 :
<div ng-controller="ListController">
다른 $ 범위와 중복 컨트롤러를 만들었습니다. 제거하면 문제가 해결되었습니다. 자세한 내용은 내 대답을 참조하십시오.
'$ resource'이''jsonp'에 대해 프로비저닝되어 있는지 확실하지 않습니다. 당신은'$ http.jsonp()'을 시도 했습니까? 서버가 실제로'jsonp'를 전달하고 있습니까? – charlietfl
시도하지는 않았지만 첫 번째 요청은 jsonp를 성공적으로 반환합니다. – Magnus
두 번째 (호출 된) 호출이 데이터를 반환합니까? 너의 시야에 뭐가있어? – marapet