Angularjs Can';使用$location.path更改视图后,无法访问$scope变量
我试图访问应用程序在单击按钮后登陆的视图中的$scope上的数据,但似乎在使用$location.path(url)进行重定向后,应用程序无法再看到$scope上存在的变量 使用按钮创建表单:Angularjs Can';使用$location.path更改视图后,无法访问$scope变量,angularjs,Angularjs,我试图访问应用程序在单击按钮后登陆的视图中的$scope上的数据,但似乎在使用$location.path(url)进行重定向后,应用程序无法再看到$scope上存在的变量 使用按钮创建表单: <form ng-submit="getBrokenProbes()"> <table class="table table-striped"> <tr> <th>
<form ng-submit="getBrokenProbes()">
<table class="table table-striped">
<tr>
<th>Bmonitor</th>
<th>Select Bmonitor</th>
</tr>
<tr ng-repeat="bmonitor in bmonitors">
<td>
<span>{{bmonitor.domainName}}</span>
</td>
<td>
<button class="btn btn-primary" ng-click="getBrokenProbes(bmonitor)">Request</button>
</td>
</tr>
</table>
</form>
我试图在不同的视图中显示该数据,但是$scope.brokenProbes在logmeinValidationResult.html(我在$location.path之后到达的页面)中不可用,因此它只显示一个空表
logmeinValidationResult.html
<table class="table table-striped">
<tr>
<th>Probe name</th>
</tr>
<tr ng-repeat="probe in brokenProbes">
<td>
<span>{{probe.description}}</span>
</td>
</tr>
</table>
I)变量$scope.brokenProbes
属于控制器logmeinValidationCtrl
其中定义了。。。
为了在另一个控制器中使用它,您应该传递它-广播
或
II)另一个(更好的)解决方案是,当用户被重定向到logmeinValidationResult
时,您可以调用API,获取数据并分配给$scope.brokenProbes
变量
那么,,
您的旧控制器应如下所示:
app.controller('logmeinValidationCtrl', ['$scope','$http', '$location', function($scope,$http, $location){
$scope.bmonitors = {};
$http.get('http://localhost/getBmonitors').success(function (data) {
$scope.bmonitors = data;
console.log($scope.bmonitors);
});
$scope.getBrokenProbes = function(bmonitor) {
$location.path('/logmeinValidationResult/' + bmonitor); // Pass bmonitor to the result here so you can call the api with that parameter later on
};
}]);
下面是新页面控制器的外观:
app.controller('logmeinValidationResultCtrl', ['$scope','$http', '$routeParams', function($scope,$http, $routeParams){
$scope.brokenProbes = [];
let url = 'http://localhost/getBrokenProbes';
let bmonitor = $routeParams.bmonitor; // Get passed bmonitor value from the route params
$http.post(url, bmonitor).then(function (response) {
$scope.brokenProbes = response.data.hosts;
console.log($scope.brokenProbes);
})
}]);
别忘了将route param
bmonitor
注册到$routeProvider或您使用的任何东西…是的,它是未定义的,因为您从未定义过它。您定义的变量位于其他控制器的作用域上。每个控制器都有自己的作用域(这就是为什么它被称为作用域)。看起来你的发帖请求实际上应该是一个GET。因此,从第二个控制器发出请求,而不是从第一个控制器发出请求。此外,您可以将GET请求移动到服务,并在整个应用程序中从该服务获取您的价值:。否则,您可以使用ui路由器之类的工具将状态传递给不同的控制器。
app.controller('logmeinValidationCtrl', ['$scope','$http', '$location', function($scope,$http, $location){
$scope.bmonitors = {};
$http.get('http://localhost/getBmonitors').success(function (data) {
$scope.bmonitors = data;
console.log($scope.bmonitors);
});
$scope.getBrokenProbes = function(bmonitor) {
$location.path('/logmeinValidationResult/' + bmonitor); // Pass bmonitor to the result here so you can call the api with that parameter later on
};
}]);
app.controller('logmeinValidationResultCtrl', ['$scope','$http', '$routeParams', function($scope,$http, $routeParams){
$scope.brokenProbes = [];
let url = 'http://localhost/getBrokenProbes';
let bmonitor = $routeParams.bmonitor; // Get passed bmonitor value from the route params
$http.post(url, bmonitor).then(function (response) {
$scope.brokenProbes = response.data.hosts;
console.log($scope.brokenProbes);
})
}]);