我正在从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"
});
});
你忘了在你的指令中注入$location
。
myApp.directive("wfDependency", function($location, countTaskService, WorkflowRuns) ...