Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular.js$http$window.location和全局变量_Http_Angularjs_Global Variables_Window.location - Fatal编程技术网

Angular.js$http$window.location和全局变量

Angular.js$http$window.location和全局变量,http,angularjs,global-variables,window.location,Http,Angularjs,Global Variables,Window.location,我试图更新$http请求中的全局变量,重定向到新页面,然后打印该变量(出错时)。我不确定是否正确执行了此操作,但当我尝试打印变量($rootscope.adHocLevel2)时,它仍然为空。 在js文件中 $scope.searchStep2 = function(option) { $http.post($scope.url, { "option" : option}). success(function(data, status) { $scope.status

我试图更新$http请求中的全局变量,重定向到新页面,然后打印该变量(出错时)。我不确定是否正确执行了此操作,但当我尝试打印变量(
$rootscope.adHocLevel2
)时,它仍然为空。 在js文件中

$scope.searchStep2 = function(option) { 
  $http.post($scope.url, { "option" : option}).
    success(function(data, status) {
      $scope.status = status;
      $scope.data = data;
      $rootScope.response = data; 
      })
    .
    error(function(data, status) {
      $scope.data = data || "Request failed";
      $scope.status = status;   
      $rootScope.response = 'Request failed';
      $rootScope.routeAd = {"route": "../adHoc/adhoc.html"};            
      $rootScope.addHocLevel2 = {"suggestedCategory": "Car","suggestedLocLabel": "world","suggestedShadowURL": "http://www.toyota.com/auris/hybrid"};
      $window.location=$rootScope.routeAd.route;            
});
};
在Html中,我正在尝试打印

<input type="text" ng-model="optLocation" value={{addHocLevel2}} />


感谢您的光临

基于上述代码,您有$window.location=$rootScope.routeAd.route,它指向“./adHoc/adHoc.html”。这将重新加载应用程序,因此您将丢失addHocLevel2值

您需要根据routeProvider.when(“/adhoc”)配置重定向到其哈希路由路径(例如#/adhoc),而不是../adhoc/adhoc.html

此外,我认为在angular的单页应用程序中移动时,最好的方法是使用$location.path(yourRoutePath)而不是$window.location,除非您想完全更改位置(新的URL)

e、 g

您有两个路由设置:

app.config(function($routeProvider){
    $routeProvider
        .when('/main', { templateUrl:'/main/main.html',controller:'MainCtrl'})
        .when('/adhoc', { templateUrl: '/adHoc/adhoc.html',controller:'AdHocCtrl});
}
然后假设您想从/main重定向到/adhoc,它是这样的:

app.controller('MainCtrl',function($rootScope,$scope,$location,$http){
    $http.post(...).error(function(data,status){
       $rootScope.addHocLevel2 = {...};
       $location.path("/adhoc"); // this will 'redirect' you to adHoc/adhoc.html page
    }
})
更新:我更新了你的plunker:
这就是我要做的——我相信还有其他方法。我使用index.html作为模板,并将main.html和adhoc.html作为其他两个页面,您可以根据URL切换到这两个页面。

您能提供一个plnkr示例吗?@user2273266谢谢您的回答。但是,我将路由更改为$rootScope.routeAd={“route”:;我发现一个错误,语法正确吗?请提供您的routerProvider设置好吗?我用更多的解释更新了上面的内容。根据routerProvider中设置的“routerProvider.when()”,尝试使用$location.path(“adhoc”)或$location.path(“adhoc.html”)。