0

如果我不注入控制器“getChildrenNumber”,一切正常,然后我从 $injector 得到错误。我什至读过这个https://docs.angularjs.org/error/$injector/unpr,但它似乎不是其中一种情况

HTML (index.html):

<html class="no-js" ng-app="myApp" ng-strict-di>
.
.
.
<script src="jsLib/angular_v1.4.js" type="text/javascript"></script>
    <script src="jsLib/angular-ui-router.min.js" type="text/javascript"></script>
    <script src="jsLib/angular-ui-router-statehelper.min.js" type="text/javascript"></script>


    <script src="app.js" type="text/javascript"></script>
    <script src="routes.js" type="text/javascript"></script>

路线.js:

var config = ["$urlRouterProvider", "$stateProvider", "$locationProvider", function($urlRouterProvider, $stateProvider, $locationProvider){

$urlRouterProvider.otherwise("/multi");

$stateProvider
    .state("multi",{
        url: "/multi",
        views: {
            "": {
                templateUrl: "multipleView.htm",
                controller: "MainCTRL",
                controllerAs: "ctrl"
            },
            "viewA@multi": {
                templateUrl: "testingBlock.htm",
                controller: "MainCTRL",
                controllerAs: "ctrl"
            },
            "viewB@multi": {
                templateUrl: "app/templates/login.htm",
                controller: ["$scope", "getChildrenNumber", function($scope, getChildrenNumber){
                    $scope.nameApp = "nameAppChanged";
                    $scope.children = getChildrenNumber + " Miao";
                }]
            }
        },
        resolve: {
            getChildrenNumber: ["$http", function($http){
                return "Some response from an API";
            }]
        }
    });
}];



angular
    .module("myApp")
    .config(config);

应用程序.js:

angular
    .module("myApp", ["ui.router"])
    .controller("MainCTRL", ["$scope", "getChildrenNumber", function($scope, getChildrenNumber){
        this.nameApp = "myApp";
        $scope.children = getChildrenNumber;
    }]);
4

1 回答 1

1

你以错误的方式实施它..

首先,在 routes.js

$stateProvider
    .state("multi",{
        url: "/multi",
        views: {
           // Views Code here //
        },
        resolve: {
            getChildrenNumber: getChildrenNumber
        }
    });
}];

那么你必须创建你的工厂:

angular.module('myApp')
.factory('children', ['$http', function ($http) {
  return {
    getChildrenNumber: getChildrenNumber
  };

  function getChildrenNumber() {
    return "Some response from an API";
  }
}]);

在此之后,您可以将其注入控制器中children

于 2015-06-10T16:36:42.193 回答