我正在使用angularJS 1.2。我使用angularJs的路由功能来部分加载一些视图。 这是我的路由代码:
var app = angular.module('app', ['ngAnimate', 'ngRoute']).config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
$routeProvider.when('/category/:name', {templateUrl: '<?php echo base_url().'home/filterPlacesView' ?>', controller: places});
$routeProvider.when('/region/:name', {templateUrl: '<?php echo base_url().'home/filterPlacesView' ?>', controller: places});
$locationProvider.html5Mode(true);
}]);
我的观点如下:
<div ng-controller="places">
<div class="sidebar">
<h3 ng-show="regions.length != 0">أماكن أخرى</h3>
<a href='<?php echo base_url().'region/'; ?>{{region.href_name}}' ng-repeat="region in regions" class='label label-danger'>{{region.name}}</a>
<h3 ng-show="cats.length != 0">تصنيفات أخرى</h3>
<a href='<?php echo base_url().'category/' ?>{{cat.href_name}}' ng-repeat="cat in cats" class='label label-info'>{{cat.name}}</a>
</div>
<div ng-view>
</div>
</div>
places
控制器看起来像这样:
function places ($rootScope, $scope, $http, $location){
$scope.places = [];
$scope.regions = [];
$scope.cats = [];
//load more places
$scope.loadMore = function(){
console.log('load more called');
}
//load categories
$scope.getCats = function(){
$scope.regions = [];
$scope.cats = [];
//get data via $http.post request, and the data returned successfully and $scope.cats filled but not reflected in the view
}
//load regions
$scope.getRegions = function(){
$scope.regions = [];
$scope.cats = [];
//get data via $http.post request, and the data returned successfully and $scope.regions filled but not reflected in the view
}
// function that loads the partial page data
$scope.getData = function(){
$scope.getCats();
$scope.getRegions();
/*get partial page data via $http.post request.
data returned successfully and $scope.places filled and reflected in the view
*/
}
//load the data
$scope.getData();
}
我需要的是当视图改变时,侧栏数据也发生了变化。但这已经在第一次加载主页面(比如index.php)和侧栏$scope.cats
和$scope.regions
填充并在视图中呈现,然后如果页面路由$scope.regions
和{{1填充但视图未更新。
我在这里找到了question,但这并没有解决问题。