1

我正在使用 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$scope.cats

我在这里找到了这个问题,但这并没有解决问题。

4

0 回答 0