Angularjs 从控制器到指令的异步数据:未知提供程序错误
我希望实现一个具有隔离作用域和双向绑定到异步加载数据的控制器作用域的指令 html的一部分:Angularjs 从控制器到指令的异步数据:未知提供程序错误,angularjs,asynchronous,angularjs-directive,angularjs-scope,Angularjs,Asynchronous,Angularjs Directive,Angularjs Scope,我希望实现一个具有隔离作用域和双向绑定到异步加载数据的控制器作用域的指令 html的一部分: <body ng-app = 'authorGraph' ng-controller = 'GraphCtrl'> <graph-dir/> 浏览互联网,我发现承诺可以做到这一点。这段代码在指令实现之前就已经出现了 authorGraph.config(function($routeProvider) { $routeProvider.when('/', {
<body ng-app = 'authorGraph' ng-controller = 'GraphCtrl'>
<graph-dir/>
浏览互联网,我发现承诺可以做到这一点。这段代码在指令实现之前就已经出现了
authorGraph.config(function($routeProvider) {
$routeProvider.when('/',
{
controller: 'GraphCtrl',
resolve: {
getData: function ($http) {
return $http.get('graph_data.json').then(function (data) {
return data;
});
}
}
});
});
authorGraph.controller('GraphCtrl', function GraphCtrl($scope, getData) {
$scope.graphData = getData;
});
然而,我被错误缠住了
未知提供程序:getDataProvider
在上面的代码中,当您将某些内容作为依赖项注入时,它将开始寻找适当的提供程序。在您的情况下,它正在寻找getDataProvider,但它无法;我找不到任何服务
使用resolve时,返回的resolve参数将在$scope对象中可用。您不需要将其作为依赖项注入
上述代码应为:
authorGraph.controller('GraphCtrl', function GraphCtrl($scope) {
$scope.graphData = $scope.getData;
});
authorGraph.controller('GraphCtrl', function GraphCtrl($scope, getData) {
$scope.graphData = getData;
});
authorGraph.controller('GraphCtrl', function GraphCtrl($scope) {
$scope.graphData = $scope.getData;
});