2016-09-19 2 views
0

ionic view 페이지 내 공장에서 반환 된 응답 데이터를 표시하는 데 문제가 있습니다. 나는이 문제가 내가 약속을 처리하는 방식과 관련이 있다고 믿지만, 나는 그 이유를 정확히 지적 할 수 없다.

공장

.factory('profileFactory', function ($http, $q) { 
var config = { 
    headers: { 
     'MyKey': 'myvalue' 
    } 
}; 
this.getEmployee = function (userId) { 
    return $http.get('http://someurlpathtogoto' + userId + 'etc', config).then(function (response) { 
     console.log(response.data) 
     return response.data; 
    }) 
} 
return this; 
}) 

위의 코드는 내가 지금 내 컨트롤러에 필요한 JSON 개체를 반환합니다 : 그래서 내 문제에 대한 로드맵을 시작하려면, 여기 내이

컨트롤러를

.controller('EmployeeCtrl', ['$scope', 'profileFactory', function ($scope, profileFactory) { 

//Set back to false 
$scope.profileLoaded = true; 
$scope.serviceUnavailable = false; 

$scope.setId = function (userId) { 

    profileFactory.getEmployee(userId).then(function(arrItems){ 
     $scope.employee = arrItems; 
     $scope.firstName = $scope.employee.record[0].First_Name; 
     $scope.lastName = $scope.employee.record[0].Last_Name; 
    }; 
}]) 

내 페이지가 표시 될 때 다음과 같이 표시하고 싶습니다.

이온보기 (프로필) 프로필 페이지로 수행해야하는 응용 프로그램의 화살표 버튼에 사용자 프레스 약간의 정보를 볼 때마다

<ion-view view-title="Profile" ng-controller="EmployeeCtrl" hide-nav-bar="true"> 
<ion-pane> 
    <!-- TODO: BACK BUTTON, NO TITLE --> 
    <ion-header-bar class="bar-stable"> 
     <h1 class="title">Ionic Blank Starter</h1> 
    </ion-header-bar> 
    <ion-content> 
     <div ng-show="serviceUnavailable"> 
      Directory service unavailable 
     </div> 
     <div ng-show="profileLoaded"> 
      <br /> 
      <center><img class="focus-img-circle" ng-src="default.png" /></center> 
      <center><b>{{firstName}} {{lastName}}</b></center> 
     </div> 
    </ion-content> 
</ion-pane> 
<ion-view> 

이 모든 호출된다. 다음 코드는 이전보기에 나타납니다.

이온보기 (검색 결과) 그래서 본질적으로 사람 탭에서 사용자가 클릭

<ion-item class="item item-avatar item-button-right" ng-repeat="user in results" ng-controller="EmployeeCtrl"> 
      <img ng-src="data:image/png;base64,{{user.pic}}"> 
      <strong>{{user.first_name}} {{user.last_name}}</strong> 
      <div class="buttons" style="padding:20px;"> 
       <button type="button" class="button button-icon ion-ios-telephone-outline customSearchIcon" ng-click=""></button> 
       <a class="button button-icon ion-ios-arrow-right customSearchIcon" ng-click="setId(user.id)" href="#/tab/search/profile"></a> 
       <button type="button" class="button button-icon ion-ios-heart-outline customSearchIcon" ng-click="addFavorite(user.id, user.first_name, user.last_name, user.pic)"></button> 
      </div> 
     </ion-item> 

내 공장이 포함 된 JSON 개체를 반환 편안한 통화를하는 데 사용되는 프로필에 소요 해당 사용자에 대한 데이터 이오니아 뷰에는 아무 것도 표시되지 않습니다. 컨트롤러에 사용자 ID을 하드 코딩하고 기능을 없애는 경우 setId() 작동하지만 분명히 옵션이 아닙니다. 누군가 내가 특별히 잘못하고있는 것을 보았습니까? 어떤 도움을 주면 몇 시간 동안 붙어 있었을 것입니다.

많은 요점을 설명하기 위해 많은 코드를 남겼습니다.

편집

표시되는 두 개의 뷰는 서로 다른 이온 템플릿입니다. 그래서 = "EmployeeCtrl이 같은보기에 두 번 발생되지 NG-컨트롤러를 사용하여.

답변

1

을 실수는, 화살표를 클릭에, 당신은 서비스를 호출 및 범위 변수에 데이터를 저장 한 후 두 번째보기로 이동합니다.을 두 번째보기는 첫 번째보기와 동일한 컨트롤러를 사용하지만 새 컨트롤러가 빈 범위로 인스턴스화되어 있으므로보기가 비어 있습니다.

<a> 태그 대신 userid를 전달하도록 프로필 경로를 수정하십시오. - tab/search/profile/:userid 및 앵커 태그에 ng-href= "#/tab/search/profile/{{user.id})"이 있고 프로필 컨트롤러에서 쿼리 문자열 ($location.search().userid)에서 userid를 가져 와서 Ajax cal을 만듭니다. 엘.

+0

빠른 응답을 가져 주셔서 감사합니다. 지금 이것을 들여다보고 그것에 대해 다시 알게 될 것입니다. 나는보기가있는 국가에 대한 약간의 독서를 했었고 그것이 그 범위에 어떻게 영향을 줄 수 있었는지 이제 훨씬 더 이해가됩니다. 조만간 연락 드리겠습니다. – Sam5487

+0

도움 주셔서 감사합니다. 비록 그것이 아닌 것으로 생각했지만 새 컨트롤러가 빈 범위로 인스턴스화되는 것에 대해 완전히 옳았습니다. 내 SearchCtrl에 코드를 추가하기 만하면 해결 방법을 찾았습니다. 분명히 나는 ​​이것을 미래에 이런 식으로하고 싶지 않고 당신이 제안한 접근법을 사용하고 싶지 않지만, 간단한 POC를 위해서 이것은 고맙게 작용할 것이다. – Sam5487

+0

@ Sam5487 - 기뻤습니다! 해피 코딩 ... – Developer

관련 문제