• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

的 Angular $routeProvider 测试.这段代码有什么问题?

用户头像
it1352
帮助1

问题说明

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

I have created a simple Angular JS $routeProvider resolve test application. It gives the following error:

Error: Unknown provider: dataProvider <- data

如果有人能指出我哪里出错了,我将不胜感激.

I would appreciate it if someone could identify where I have gone wrong.

index.html

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

ResolveTest.js

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>

正确答案

#1

问题是你在 index.html 中的 body 标签上有 ng-controller="ResolveCtrl" 而在 $routeProvider 为 rt.html 指定相同的控制器.从 body 标签中取出控制器定义,让 $routeProvider 处理它.之后效果很好.

The problem is that you have ng-controller="ResolveCtrl" on your body tag in index.html when also in your $routeProvider you specify the same controller for rt.html. Take out the controller definition from your body tag and just let the $routeProvider take care of it. It works great after that.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcghfak
系列文章
更多 icon
同类精品
更多 icon
继续加载