2014-04-17 26 views
3

我想知道是否有可能使用angluarjs爲單個url視圖使用多個控制器,但我一直無法找到關於此的很多文檔。我想用一個控制器上的所有頁面切換頁面標題標題,但某些頁面已經包含控制器一個視圖的多個控制器angularjs

app.js

subscriptionApp.config(['$routeProvider',function($routeProvider){ 
$routeProvider. 
when('/billinginfo',{templateUrl:'views/billing-info.html', controller:'billingInfoController'}). 
when('/orderreview',{templateUrl:'views/order-review.html', controller:'billingInfoController'}). 
when('/subscribed',{templateUrl:'views/subscribed.html', controller:'subscribedTitle'}). 

//EXAMPLE: HOW COULD I ADD TWO CONTROLLERS TO SAME PAGE??? THIS DOES NOT WORK 
when('/subscribe',{templateUrl:'views/subscribe.html', controller:'subscriptionController', 'testControllerTitle'}). 

when('/unsubscribed',{templateUrl:'views/cancelconfirm.html', controller:'unsubscribedTitle'}). 
when('/redirectBack',{templateUrl:'views/redirect-to-app.html'}). 
when('/redirectHandler',{templateUrl:'views/redirect-handler.html',controller:'redirectController'}). 
when('/error',{templateUrl:'views/error.html', controller:'messageController'}). 
otherwise({redirectTo:'/subscribe'}); 
}]); 

編輯 我想添加標題控制器每個頁面視圖:

function testControllerTitle($rootScope, $scope, $http) { $rootScope.header = "Success!"; } 

如果我這個控制器添加到不已經有一個控制器,它的工作原理,如果有一個地方另一個控制器我不能做這項工作的頁面。

<h1 ng-bind="header"></h1> 
+0

爲什麼沒有一個單一的控制器是你的模板頭的一部分,並使用服務來更新它?像這樣:https://github.com/JeremyLikness/AngularRoutes/blob/master/Scripts/app/services/titleService.js –

回答

4

是,控制器和模板是獨立的,請在此http://jsbin.com/wijokuca/1/

var app = angular.module("App", ['ngRoute']); 

app.config(function ($routeProvider) { 
    $routeProvider 
    .when('/a', {templateUrl: 'this.html', controller: "aCtrl"}) 
    .when('/b', {templateUrl: 'this.html', controller: "bCtrl"}) 
    .when('/c', {templateUrl: 'that.html', controller: "bCtrl"}) 
    .otherwise({redirectTo: '/a'}); 
}); 

app.controller('aCtrl', function ($scope) { 
    $scope.all = [1,2,3]; 
}); 

app.controller('bCtrl', function ($scope) { 
    $scope.all = [4,5,6]; 
}); 
相關問題