本文介绍了简单角$ routeProvider决心的考验。有什么不对这个code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的角度JS $ routeProvider决心测试应用程序。它提供了以下错误:

 错误:未知提供商:dataProvider中<  - 数据

我想AP preciate,如果有人可以找出我已经错了。

index.html的

 <!DOCTYPE HTML>
< HTML NG-应用=ResolveTest>
  < HEAD>
    <标题>解决测试与LT; /标题>
    &所述; SCRIPT SRC =htt​​ps://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js> < / SCRIPT>
    &所述; SCRIPT SRC =ResolveTest.js>&下; /脚本>
  < /头>
  <机身NG控制器=ResolveCtrl>
    < D​​IV NG-视图>< / DIV>
  < /身体GT;
< / HTML>

ResolveTest.js

  VAR RT = angular.module(ResolveTest,[]);rt.config([$ routeProvider功能($ routeProvider)
{
  $ routeProvider.when(/,{
    templateUrl:rt.html
    控制器:ResolveCtrl
    解析:{
      数据:[$ Q,$超时功能($ Q $超时)
      {
        变种推迟= $ q.defer();        $超时(函数()
        {
          deferred.resolve(我的数据);
        },2000);        返回deferred.promise;
      }]
    }
  });
}]);rt.controller(ResolveCtrl,[$范围,数据功能($范围,数据)
{
  的console.log(数据+数据);
  $ scope.data =数据;
}]);

rt.html

 <跨度> {{}数据}< / SPAN>


解决方案

的问题是,你有 NG-控制器=ResolveCtrl在索引你的身体标记。当还在你的$ routeProvider您指定rt.html相同的控制器HTML。从你的身体标记取出控制器的定义,只是让$ 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.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>
解决方案

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.

这篇关于简单角$ routeProvider决心的考验。有什么不对这个code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 20:58