条件付きビューを達成するためのアンギュラUIルータ
-
21-12-2019 - |
質問
この質問に似たような質問をしています: UIルーター条件付きUIビューのビュー?しかし、私の状況はもう少し複雑で、提供された答えを手に入れてもらうことはできません。
基本的に、URLが指すエンティティの種類に応じて、2つの非常に異なる方法をレンダリングできるURLを持っています。
$stateProvider
.state('home', {
url : '/{id}',
resolve: {
entity: function($stateParams, RestService) {
return RestService.getEntity($stateParams.id);
}
},
template: 'Home Template <ui-view></ui-view>',
onEnter: function($state, entity) {
if (entity.Type == 'first') {
$state.transitionTo('home.first');
} else {
$state.transitionTo('home.second');
}
}
})
.state('home.first', {
url: '',
templateUrl: 'first.html',
controller: 'FirstController'
})
.state('home.second', {
url: '',
templateUrl: 'second.html',
controller: 'SecondController'
});
.
RESTFULサービスから実際のエンティティを取得するための解決を設定しました。 私が実際にタイプに基づいてトランジションに到達するまで、すべてのことが働いているようです。
IDがNULLのため、Realve Re-FiresとGetEntityが失敗することを除いて、遷移が機能します。
私はIDを遷移に送信しようとしましたが、それでもそれがまだ2番目の解決を行おうとしています。つまり、エンティティはRESTサービスから2回取得されます。
解決
templateurl は、機能を確認して別の違いを返します。状態設定の一部ではなく、ビュー内のコントローラを表示および定義します。TemplateRoviderを使用する必要がある可能性があるようにTemplateURLにパラメータを注入することはできません。
$stateProvider.state('home', {
templateProvider: ['$stateParams', 'restService' , function ($stateParams, restService) {
restService.getEntity($stateParams.id).then(function(entity) {
if (entity.Type == 'first') {
return '<div ng-include="first.html"></div>;
} else {
return '<div ng-include="second.html"></div>';
}
});
}]
})
. 他のヒント
次のこともできます。
$stateProvider
.state('home', {
url : '/{id}',
resolve: {
entity: function($stateParams, RestService) {
return RestService.getEntity($stateParams.id);
}
},
template: 'Home Template <ui-view></ui-view>',
onEnter: function($state, entity) {
if (entity.Type == 'first') {
$timeout(function() {
$state.go('home.first');
}, 0);
} else {
$timeout(function() {
$state.go('home.second');
}, 0);
}
}
})
.state('home.first', {
url: '',
templateUrl: 'first.html',
controller: 'FirstController'
})
.state('home.second', {
url: '',
templateUrl: 'second.html',
controller: 'SecondController'
});
. 親コントローラを親ではなく、最初と2番目の兄弟を兄弟にすることになった後、解決した結果に応じて1または2番目まで自宅のコントローラを実行させました。
UI-Routeの条件付きビューの検証コードを使用する
$stateProvider.state('dashboard.home', {
url: '/dashboard',
controller: 'MainCtrl',
// templateUrl: $rootScope.active_admin_template,
templateProvider: ['$stateParams', '$templateRequest','$rootScope', function ($stateParams, templateRequest,$rootScope) {
var templateUrl ='';
if ($rootScope.current_user.role == 'MANAGER'){
templateUrl ='views/manager_portal/dashboard.html';
}else{
templateUrl ='views/dashboard/home.html';
}
return templateRequest(templateUrl);
}]
});
. 所属していません StackOverflow