Javascript Angularjs中$location的问题
我有一个单页应用程序(Python/Django中的后端),其中我的函数返回json响应,该json响应由前端的angular js处理。我正在使用angular ajax调用来命中函数。现在我们都知道,在ajax调用中,地址栏中的url不会改变。但在angular js中,我们可以使用Javascript Angularjs中$location的问题,javascript,jquery,ajax,django,angularjs,Javascript,Jquery,Ajax,Django,Angularjs,我有一个单页应用程序(Python/Django中的后端),其中我的函数返回json响应,该json响应由前端的angular js处理。我正在使用angular ajax调用来命中函数。现在我们都知道,在ajax调用中,地址栏中的url不会改变。但在angular js中,我们可以使用$location.path()设置url。所以它保留了我访问过的url的历史记录,在浏览器的“后退”按钮上,它将地址栏中的url更改为上一个。但它不会改变页面的内容 我的电话: app.controller(
$location.path()
设置url。所以它保留了我访问过的url的历史记录,在浏览器的“后退”按钮上,它将地址栏中的url更改为上一个。但它不会改变页面的内容
我的电话:
app.controller('myController',
function($scope,$http, $location, $route, $timeout){
$scope.formData={}
$scope.getAllBrainframes=function(id){
if(id){
$scope.url = '/get-brainframe/'+id+'/';
}else{
$scope.url = '/get-brainframe/';
}
$http.post($scope.url)
.success(function(data,status){
.success(function(data,status){
$scope.title = data[0].title
$scope.brainframes = data;
$location.path($scope.url);
$scope.parent= data[0].id;
})
.error(function(data,status){
});
}
});
当我在ajax成功上设置$location.path()
时,它会在地址栏中附加当前访问的url,并保存我访问过的每个url的历史记录。但当我点击browser back按钮时,地址栏中的url会变为上一个,而不是内容
现在,当我点击浏览器后退按钮时,是否有任何功能可以触发,或者如何更改页面内容
编辑:
以上ajax成功函数已编辑
我的html:
<div class="content">
<span ng-repeat="brainframe in brainframes">
<p ng-if = "brainframe.brainframes.length > 0 ">
<ul class="list-group col-md-5">
<div data-ng-repeat="brain in brainframe.brainframes" class="child-brainframes">
<a class="my-title" 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>
</div>
</ul>
</p>
<p ng-if = "brainframe.brainframes.length < 1 ">
<span>No brainframes are available.</span>
</p>
</span>
</div>
- {$brain.count\u children$}{$brain.brainframe\u child.title$}
没有可用的大脑框架。
您需要查看$routeParams
并更改模板中的内容
.controller("MainCtrl",
function($scope,$http, $location, $route, $timeout, $routeParams){
$scope.formData={};
$scope.id = $routeParams.id;
$scope.getAllBrainframes=function(id){
if(id){
$scope.url = '/home/'+id+'/';
}else{
$scope.url = '/home';
}
console.log($scope.url);
$location.path($scope.url);
};
});
在模板中:
检查$scope
属性并显示/隐藏
<script type="text/ng-template" id="Home.html">
<p>This is one template</p>
<p ng-if="id">This is in next template</p>
<a ng-click="getAllBrainframes('1')">next template</a>
</script>
这是一个模板
这在下一个模板中
更新:
如果要在路由之间持久化ajax调用数据,您可能需要将其存储在服务中,并根据服务中的$routeParams
在控制器中访问它。尝试使用$routeParams检查id并在服务中显示不同的内容page@mohammedrias在我的例子中,ajax调用很重要,调用的响应被传递到html,这样我就可以显示我的数据。我已经编辑了我的问题,请检查并让我知道您的想法是否仍然可行。每次路由更改时,控制器都将重新加载。您必须将ajax调用的数据存储在服务中,然后尝试在控件中访问它。当我单击“浏览器后退”按钮时,命令正在更改,但控制器没有重新加载控制器将在每次路线更改时重新加载。尝试放置console.log消息,然后自己查看。每次路由更改时,路由提供程序都会运行并检查需要加载的控制器/模板