一尘不染

简单的Angular $ routeProvider解决测试。此代码有什么问题?

angularjs

我创建了一个简单的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>

阅读 169

收藏
2020-07-04

共1个答案

一尘不染

问题在于,ng-controller="ResolveCtrl"当您在$
routeProvider中也为rt.html指定相同的控制器时,您就在index.html的body标签上了。从body标记中删除控制器定义,然后让$
routeProvider来处理它。之后效果很好。

2020-07-04