나는 uaContext
에 productId
및 operatorId
과 같은 유용한 정보가있는 디자인을 수행했습니다. 각 페이지의 내용을 표시하려면이 정보가 필요합니다.설정 변수에서 Angularjs 약속 및 설계 문제
myApp.service('uaContext', function($cookieStore){
this.init = function() {
this.isLogged = new Field();
this.userName = new CookieField('userName', $cookieStore);
this.appName = new Field();
this.subAppName = new Field();
this.operator = new Field();
this.operatorList = new FilterField();
this.product = new Field();
this.productList = new FilterField();
};
this.init();
});
, 나는 세계 $routeChangeStart
이벤트 잡을 내가 정보를 가지고 있는지 확인하려면 내가 쿠키에 세션 덕분에 복구 할 때이 경우
myApp.run(function($rootScope, $routeParams, uaViewProvider, uaAuthService, uaContext){
$rootScope.$on('$routeChangeStart', function (event, next) {
if (uaContext.isLogged.get() == false && next.loginRequired != false){
console.log('View requires to be logged in');
if (uaAuthService.sessionCookieExists() == true){
console.log('Recover session from Cookie');
uaAuthService.loadSession().then(function() {
uaContext.operator.set(uaContext.operatorList.filter('id', +$routeParams.operatorId));
uaContext.productList.set(uaContext.operator.get().products);
uaContext.product.set(uaContext.productList.filter('id', +$routeParams.productId));
});
} else{
console.log('User not logged in. Redirect to login.');
uaViewProvider.redirectToView('login');
}
}
});
});
을, 내가 가진 세션을 다시로드 요청 loadSession
약속. 그런 다음 url 매개 변수를 기반으로 productId
및 operatorId
을 정의합니다.
var myApp2 = angular.module('ua.myApp2', []);
myApp2.controller('ua.myApp2Controller',
function ($scope, $rootScope, $routeParams, uaContext, uaApiInterface) {
$scope.$on('$routeChangeSuccess', function() {
uaContext.appName.set('Settings');
uaContext.subAppName.set('Sims');
});
console.log('On route change');
$scope.$watch(function(){
watchValue = uaContext.operator.get().id + '-' + uaContext.product.get().id
return watchValue;
}, function(){
uaApiInterface.getSimList(uaContext.operator.get().id,uaContext.product.get().id).then(
function(objects) {
console.log('Then');
$scope.simList = objects.data.objects;
console.log($scope.simList);
console.log(objects.data.objects);
}
)
},true);
}
);
나는 loadSession
약속의 끝을 기다려야 발견 유일한 해결책은 operatorId
에 시계를 넣어하는 것입니다
문제는보기에 좀 더 정보를 요청하기 전에이 매개 변수를 얻을 필요가있다 및 productId
.
문제는이 시계를 에 대한 모든 시계를으로 정의해야한다는 것입니다. 이것은 정말 건조하지 않기 때문에 내 질문에 어떻게 이것을 최적화 할 수 있습니까? 컨텍스트 서비스에서
uaAuthService.loadSession().then(function() {
uaContext.operator.set(uaContext.operatorList.filter('id', +$routeParams.operatorId));
uaContext.productList.set(uaContext.operator.get().products);
uaContext.product.set(uaContext.productList.filter('id', +$routeParams.productId));
})
:
우리가 알고있는 것처럼 몇 가지 옵션이 있습니다 : 지시어를 만들고 거기에'$ watch'를 구현하십시오. 모든보기에 하나의 컨트롤러 만 사용 (단편과 같은 일명보기 사용) –
감사합니다. 라우팅을 완료하겠다는 약속의 끝을 기다리는 것이 가능하지 않습니까? 동기식 호출이 해결책이 될 수 있지만 Angularjs에서는 사용할 수없는 것 같습니다 ... – Julio