我创建了一个简单的Angular JS $ routeProvider解决测试应用程序。它给出以下错误:

Error: Unknown provider: dataProvider <- data

如果有人可以确定我哪里出了问题,我将不胜感激。

index.html
<!DOCTYPE html>
<html ng-app="ResolveTest">
  <head>
    <title>Resolve Test</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js">    </script>
    <script src="ResolveTest.js"></script>
  </head>
  <body ng-controller="ResolveCtrl">
    <div ng-view></div>
  </body>
</html>

ResolveTest.js
var rt = angular.module("ResolveTest",[]);

rt.config(["$routeProvider",function($routeProvider)
{
  $routeProvider.when("/",{
    templateUrl: "rt.html",
    controller: "ResolveCtrl",
    resolve: {
      data: ["$q","$timeout",function($q,$timeout)
      {
        var deferred = $q.defer();

        $timeout(function()
        {
          deferred.resolve("my data value");
        },2000);

        return deferred.promise;
      }]
    }
  });
}]);

rt.controller("ResolveCtrl",["$scope","data",function($scope,data)
{
  console.log("data : " + data);
  $scope.data = data;
}]);

rt.html
<span>{{data}}</span>

最佳答案

问题是,当您也在$ routeProvider中为rt.html指定相同的 Controller 时,在index.html的body标记上具有ng-controller="ResolveCtrl"。从body标记中删除 Controller 定义,然后让$ routeProvider处理它。之后效果很好。

关于angularjs - 简单的Angular $ routeProvider解决测试。此代码有什么问题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16849050/

10-12 07:22