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

时间:2019-04-24 08:58:25

标签: angularjs error-handling routes

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

我尝试了$location.path("404"),但总是收到错误消息:

  

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

指令:

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"
    });
  });

1 个答案:

答案 0 :(得分:1)

您忘记在指令中注入$location

myApp.directive("wfDependency", function($location, countTaskService, WorkflowRuns) ...