如何重定向到AngularJS中的错误页面?

问题描述 投票:-1回答:1

我正在从JSON加载数据,如果数据不可用,我想重定向到错误页面。

我试过$location.path("404"),但我总是收到一条错误信息:

“未捕获的TypeError:无法读取未定义的属性'路径'”。

指示:

myApp.directive("wfDependency", function(countTaskService,WorkflowRuns) {
  return {
    some d3 code in here...
  }
  $scope.$watch("workflowPath", function(wfPath, wfPath_old) {              
    d3.json("json/workflows/"+$scope.workflowPath+".json", function(error, data, $location) {
      if(error){
       console.log("ERROR");
       $location.path("/404");
      }else{
        $scope.data = data;
        createGraph();
      }
    });
  });
}); 

对myApp:

let myApp = angular.module("myApp", ["ngRoute", "ngResource"])
  .config(function($routeProvider) {
    $routeProvider
    .when("/404", {
      templateUrl: "views/404.htm"
    });
  });
angularjs error-handling routes
1个回答
1
投票

你忘了在你的指令中注入$location

myApp.directive("wfDependency", function($location, countTaskService, WorkflowRuns) ...
© www.soinside.com 2019 - 2024. All rights reserved.