AngularJS UI-Router一个视图分别由多个控制器使用

时间:2016-06-22 08:15:30

标签: angularjs

我在stackoverflow上没有找到关于这个具体问题的答案。

在UI-Router中可以并且可以为2个不同的状态指定相同的view / templateUrl吗?

.state('app.dashboardLanding', {
    url: '/dashboard-landing',
    title: 'Dashboard Overview',
    templateUrl: 'app/views/landing.html',
    controller: 'DashboardLandingController',
})
.state('app.balanceSheetLanding', {
    url: '/balance-sheet-landing',
    title: 'Balance sheet Overview',
    templateUrl: 'app/views/landing.html',
    controller: 'BalanceSheetLandingController'
})

app/views/landing.html中的代码完全相同,只有$scope属性不同。

1 个答案:

答案 0 :(得分:0)

<强> app.js

'use strict';
// Declare app level module which depends on filters, and services
var app= angular.module('myApp', ['ngRoute']);
app.config(['$routeProvider', function($routeProvider) {
  $routeProvider.when('/login', {templateUrl: 'partials/login.html', controller: 'loginCtrl'});
  $routeProvider.when('/home', {templateUrl: 'partials/home.html', controller: 'homeCtrl'});
  $routeProvider.when('/salesnew', {templateUrl: 'partials/salesnew.html', controller: 'salesnewCtrl'});
  $routeProvider.when('/salesview', {templateUrl: 'partials/salesview.html', controller: 'salesviewCtrl'});
  $routeProvider.when('/users', {templateUrl: 'partials/users.html', controller: 'usersCtrl'});
    $routeProvider.when('/forgot', {templateUrl: 'partials/forgot.html', controller: 'forgotCtrl'});


  $routeProvider.otherwise({redirectTo: '/login'});


}]);


app.run(function($rootScope, $location, loginService){
    var routespermission=['/home'];  //route that require login
    var salesnew=['/salesnew'];
    var salesview=['/salesview'];
    var users=['/users'];
    $rootScope.$on('$routeChangeStart', function(){
        if( routespermission.indexOf($location.path()) !=-1
        || salesview.indexOf($location.path()) !=-1
        || salesnew.indexOf($location.path()) !=-1
        || users.indexOf($location.path()) !=-1)
        {
            var connected=loginService.islogged();
            connected.then(function(msg){
                if(!msg.data)
                {
                    $location.path('/login');
                }

            });
        }
    });
});