2015-01-04 1 views
0

나는 editingTimesheet와 allowed_times (이 것은 editingTimesheet의 날짜를 기준으로 계산 됨) 목록이 필요한 템플릿을 가지고 있습니다.

나는 컨트롤러의 코드 만에 사용되는 I는 $의 state.go을 수행 할 때 (

가) 나는 상태에서 그 결의를 (본 비디오 년대 지식인을보고 한 번이 아니라 실행하는 것 같다 발견 폼을 열기 전에로드 할 데이터를 강제로로드하십시오. 내가 지금 가지고있는 코드를 사용하면 editingTimesheet가로드되기 전에 $ scope.timesheet가 설정됩니다 (allowed_times와 동일합니다.)

내 서비스에서 이 코드를 가지고 :

.factory('Timesheets', function($resource, $rootScope, LinkData, Company) { 
    var Timesheet = $resource('http://127.0.0.1:3000/api/v1/timesheets/:id',{id:'@id'}, {update:{method:'PUT'}}); 
    var _allowed_times; 

    return { 
    timesheet: function() { 
     return Timesheet; 
    }, 
    allowed_times: function() { 
     return this._allowed_times 
    }, 
    get: function(id){ 
     return Timesheet.query({q: {id_eq: id}}).$promise.then(function (data, status) { 
     this.editingTimesheet = data; 
     Company.allowed_times(data.currentDate).then(function(data2) { //fill allowed times 
      this._allowed_times = data2; 
     }); 
     }); 
    } 
    } 
}) 

컨트롤러 나는이 있습니다

var TimesheetDetailCtrl = app.controller('TimesheetDetailCtrl', function($scope, Timesheets) { 
    $scope.allowed_times = Timesheets.allowed_times(); 
    $scope.timesheet = Timesheets.editingTimesheet; 
} 

나는 또한 내가 .STATE에 해결하기 위해 매핑이 기능을 가지고있다 :

TimesheetDetailCtrl.beforeShow = function($q, $stateParams, Timesheets) { 
    Timesheets.get($stateParams.timesheetId); 
}; 

을 마지막으로 나는이 상태가 :

.state('timesheet-detail', { 
    url: '/timesheet/:timesheetId', 
    templateUrl: 'templates/timesheet-detail.html', 
    controller: 'TimesheetDetailCtrl', 
    resolve: { 
     reset: TimesheetDetailCtrl.beforeShow 
    } 
}) 

을 추가 질문 : beforeShow 함수는 템플릿이로드 될 때마다 실행됩니다. 컨트롤러의 코드하지 :

$scope.timesheet = Timesheets.editingTimesheet; 

오전 나는 가정 올바른 그 Timesheets.editingTimesheet 변경, 자동 또한 $의 scope.timesheet을 변경하고 컨트롤러의 코드를 실행할 필요가 없습니다 이유입니다 때 매번?

답변

0

여기 몇 가지 문제가 있습니다.

  1. 이렇게 공장에 $rootScope을 전달할 수 없습니다. $rootScope 또는 '$ scope'를 컨트롤러 등의 팩토리 메서드에 전달할 수 있습니다. 공장은 싱글 톤입니다. 예 : Timesheets.allowed_times($scope)

  2. 해결 정의가 적절하지 않은 것으로 나타납니다.

는 해결 옵션은 키가 종속의 이름이 에 컨트롤러를 삽입 할 수있는 객체를 취하고 값은 해결 될 수있는 공장이다. 따라서 컨트롤러가 초기화되기 전에 해결됩니다.

나는 당신이 여기 TimesheetDetailCtrl.beforeShow 방법이 필요 없습니다 당신의 코드

.state('timesheet-detail', { 
    url: '/timesheet/:timesheetId', 
    templateUrl: 'templates/timesheet-detail.html', 
    controller: 'TimesheetDetailCtrl', 
    resolve: { 
     reset: ['$stateParams', 'Timesheets', function($stateParams, Timesheets){ 
     Timesheets.get($stateParams.timesheetId); 
     }; 
    } 
}) 

에 다음과 같이 변경해야 할 것 같아요.

또는 내가 믿는

그냥 아래로 컨트롤러의 생성자를 수정하고 추가 질문에 대해서는 resolve

var TimesheetDetailCtrl = app.controller('TimesheetDetailCtrl', ['$scope', '$stateParams', 'Timesheets', function($scope, $stateParams, Timesheets) { 
    Timesheets.get($stateParams.timesheetId); 
    $scope.allowed_times = Timesheets.allowed_times(); 
    $scope.timesheet = Timesheets.editingTimesheet; 
])}; 

을 제거 할 수 있습니다

... 는 컨트롤러 생성자는 각 템플릿로드에서 실행 .

$scope.timesheet = Timesheets.editingTimesheet; 

당신은 항상 새로운 '데이터'객체를 얻기로 Timesheets.editingTimesheet 변경, $의 scope.timesheet 귀하의 경우에 변경되지 않습니다

. 참조를 그대로 유지하면 해당 참조 만 작동합니다.

editingTimesheet를 데이터가 포함 된 개체로 변경해야합니다. 예 :

Timesheets.editingTimesheet = {tsInstance: data} 
$scope.timesheet = Timesheets.editingTimesheet; 
in html, access with 
{{timesheet.tsInstance}} 
0

마침내 대답을 찾았습니다. 나는 이오니아 베타 14를 사용하고 있으며 캐시 된보기를 사용하도록 설정했습니다.

은 자세한 내용은 여기를 참조하십시오 : http://ionicframework.com/docs/api/directive/ionNavView/

관련 부분 : 우리는 이러한 견해를 캐싱하기 때문에, 우리는 범위를 파괴되지 않도록 참고. 대신 범위가 감시주기에서 연결 해제됩니다. 범위가 소멸 및 다시 작성되지 않으므로 컨트롤러가 후속보기에서 다시로드되지 않습니다. 상태 제공자 모두에서 캐싱을 사용하지 않도록 설정 한 후

는 예상대로 일 :

상태 제공자 내

안 캐시

$stateProvider.state('myState', { 
    cache: false, 
    url : '/myUrl', 
    templateUrl : 'my-template.html' 
}) 
관련 문제