我下面这个教程给予路由控制器优先,一切都很顺利:http://scotch.io/tutorials/javascript/single-page-apps-with-angularjs-routing-and-templating在AngularJS
除了我$scope.message
正在改变罚款和/contact
,同时增加在完全相同的方式一个新的作用域($scope.prodoc
)不加工。相反,它会在每个页面上显示mainController作用域。
这里有三个控制器:
calculatorApp.controller('aboutController', function($scope, $http) {
//define the hardware data
$scope.prodoc = 'companyb';
$scope.message = 'Look! I am an about page.';
});
calculatorApp.controller('contactController', function($scope, $http) {
$scope.prodoc = 'companya';
$scope.message = 'Contact us! JK. This is just a demo.';
});
// create the controller and inject Angular's $scope
calculatorApp.controller('mainController', function($scope, $http) {
//define the hardware data
$scope.prodoc = 'sdf';
// create a message to display in our view
$scope.message = 'Everyone come and see how good I look!';
body标签是在<body ng-controller="mainController">
我猜测的mainController
正在采取优先,因此不更新每个路由控制器。
我该如何强制它采取路线特定值?
编辑: 这里的路由器配置:
calculatorApp.config(function($routeProvider) {
$routeProvider
// route for the home page
.when('/', {
templateUrl : 'pages/home.html',
controller : 'mainController'
})
// route for the about page
.when('/about', {
templateUrl : 'pages/about.html',
controller : 'aboutController'
})
// route for the contact page
.when('/contact', {
templateUrl : 'pages/contact.html',
controller : 'contactController'
});
});
虽然我实际上没有任何的模板来显示。我只需要为数据(prodoc
)
你能告诉模板? –
和'$ routeProvider'配置? –
@IlanFrumer - Mah nishma。我添加了路由。该模板可以忽略不计,因为我不需要任何视图的路由。仅将数据传递到主控制器。我做错了吗? – itamar