Angularjs:如何从解析routeProvider注入依赖项

时间:2015-03-11 21:12:50

标签: angularjs routes dependencies code-injection resolve

我在将路由中的解析参数注入控制器时遇到问题。我将解析值设置为对象{name: 'Banner', slug: 'banner'},但是我收到错误。

App.js

var app = angular.module('CMS', ['fields', 'ngRoute']);

app.controller('ModuleController', ['$http', 'properties',
  function($http, properties) {
    var module = this;
    module.properties = properties;

    if (module.properties.slug.length) {
      $http.get(module.properties.slug + '.php').success(function(data) {
        module.list = data;
      });
    }
  }
]);

app.controller('HomeController', function() {});

app.config(function($routeProvider) {
  $routeProvider
  // route for the banner page
  .when('/banner1', {
    templateUrl: 'banner1.php',
    controller: 'ModuleController',
    resolve: {
      properties: function() {
        return { name: 'Banner', slug: 'banner' };
      }
    }
  })
  .when('/home', {
    templateUrl: 'home.php',
    controller: 'HomeController'
  })
  .otherwise({
    redirectTo: '/home'
  });
});

错误:

 Error: [$injector:unpr] http://errors.angularjs.org/1.3.14/$injector/unpr?p0=propertiesProvider%20%3C-%20properties%20%3C-%20ModuleController
    at Error (native)
    at http://modelo2.desenvolvimento.com/adm/js/angular/angular.min.js:6:417
    at http://modelo2.desenvolvimento.com/adm/js/angular/angular.min.js:38:7
    at Object.d [as get] (http://modelo2.desenvolvimento.com/adm/js/angular/angular.min.js:36:13)
    at http://modelo2.desenvolvimento.com/adm/js/angular/angular.min.js:38:81
    at d (http://modelo2.desenvolvimento.com/adm/js/angular/angular.min.js:36:13)
    at Object.e [as invoke] (http://modelo2.desenvolvimento.com/adm/js/angular/angular.min.js:36:283)
    at $get.w.instance (http://modelo2.desenvolvimento.com/adm/js/angular/angular.min.js:75:451)
    at http://modelo2.desenvolvimento.com/adm/js/angular/angular.min.js:58:476
    at s (http://modelo2.desenvolvimento.com/adm/js/angular/angular.min.js:7:408) <div ng-view="" class="ng-scope">

4 个答案:

答案 0 :(得分:11)

ngRoute支持将已解析的变量注入控制器,这对于应用程序的横切关注非常有用,例如应用程序的身份验证或配置。

缺点是控制器可以实例化这些可用于注入的参数,这意味着要么手动实例化控制器(使用$controller),这几乎从不案例,或者ngRoute有决心。你不能用这样的控制器做什么用ng-controller实例化它,或者在注入参数不可用的任何其他位置实例化。

此错误表示除了在路径上定义了控制器之外,您还在路径模板中将控制器定义为ng-controller。控制器的第二次实例化失败了。

答案 1 :(得分:8)

您可以使用$ route service在控制器中获取已解析的数据。

请在此处查看演示http://plnkr.co/edit/2oID3G0QStTOGEPPLQ3h?p=preview

所以在你的例子中它将如下所示:

.when('/banner1', {
    templateUrl: 'banner1.php',
    controller: 'ModuleController',
    resolve: {
      properties: function() {
        return { name: 'Banner', slug: 'banner' };
      }
    }
  })

并在控制器中:

app.controller('ModuleController', ['$http', '$route',
  function($http, $route) {
    var module = this;

   //get resolved properties
    module.properties = $route.current.locals.properties;

    if (module.properties.slug.length) {
      $http.get(module.properties.slug + '.php').success(function(data) {
        module.list = data;
      });
    }
  }
]);

答案 2 :(得分:2)

resolve : {
    properties : ['projects', 'user', function (projects, user) {
        return user.getData().then(function () {
            return projects.getData();
        });
    }]
}

答案 3 :(得分:0)

在ng-route中使用依赖注入 试试这个;

var myApp = angular.module("myApp", ["ngRoute", "localytics.directives"]);

myApp.config(['$routeProvider',function ($routeProvider) {

    $routeProvider
        .when("/", {
            templateUrl: "/TimeLine.html",
            controller: "MainCtrl"
        })    
        .when("/AddOrEditOccasion", {
            templateUrl: "/Html/GiftWhiz/AddOrEditOccasion.html",
            controller: "AddOrEditOccasionCtrl"
        })
        .when("/OccasionStart", {
            templateUrl: "/OccasionStart.html",
            controller: "OccasionStartCtrl"
        })        
}]);


myApp.controller("MainCtrl", ['$scope', '$http', '$location', function ($scope, $http, $location) {

   //your codes

}]);