角度js中$ location的问题

时间:2015-03-16 08:34:03

标签: angularjs angularjs-routing

我有一个使用Djangoangular js开发的应用。当我在浏览器中单击$locationurl is getting changed in address bar后面的后退按钮时,我将content is not getting changed in page存储在角度js中。

我的要求是地址栏中可能会或可能不会更改网址,但是当我点击后退按钮时,必须更改页面内容。

角度js提供的任何建议或功能?

已编辑:我的代码

Angular js代码:

var brainframeApp = angular.module('brainframeApp',['ngRoute']);


//configuring angularjs symbos to not to conflict with Django template symbols
brainframeApp.config(function($interpolateProvider, $locationProvider,$routeProvider) {
  $interpolateProvider.startSymbol('{$');
  $interpolateProvider.endSymbol('$}');
  $locationProvider.html5Mode(true).hashPrefix('!');

});



// Controller which contains Angularjs Functions for a/many templates or Scope.
brainframeApp.controller('brainframeController',function($scope,$http, $location, $route){
    $scope.getAllBrainframes=function(id){
        $scope.is_exist = 1
        if(id){
            $scope.url = '/get-brainframe/'+id+'/'; 
        }else{
           $scope.url = '/get-brainframe/';
        }

        $location.path($scope.url);
        aa = $route.reload();
        console.log(aa);

        $http.get($scope.url)
            .success(function(data,status){
                $scope.title = data[0].title
                var check_data = data[0].brainframes;
                if ( typeof check_data == 'undefined' || check_data.length < 1){
                    $scope.is_exist = 0;
                }
                $scope.brainframes = data;
            })
            .error(function(data,status){
            });
    }
});

HTML code:

 <div class="content">
        <div ng-if = "is_exist == 1">
            <span ng-repeat="brainframe in brainframes">
                    <p>
                        <ul class="list-group col-md-4">
                            <span data-ng-repeat="brain in brainframe.brainframes">
                                <a ng-href="" ng-click="getAllBrainframes(brain.brainframe_child.pk)"><li class="list-group-item"><span class="badge">{$ brain.count_children $}</span>{$ brain.brainframe_child.title $}</li></a>
                            </span>
                        </ul>
                    </p>
            </span>
        </div>

        <div ng-if = "is_exist == 0">
            <p>No brainframes are available.</p>
        </div>

    </div>

1 个答案:

答案 0 :(得分:0)

设置位置后尝试$route.reload();

示例代码:

controller($scope,$location,$route)
{
 $location.path("/home");
 $route.reload();
}

或者您应该在$ apply()方法中运行表达式作为函数,如

$rootScope.$apply(function() {    
        $location.path("/home");
        console.log($location.path());
      });