简体   繁体   English

角度路线没有发射控制器

[英]Angular route not firing controller

I'm having issue getting the controllers for my routes to execute. 我在获取执行路由的控制器时遇到问题。

Basically, I register my routes as follows: 基本上,我按如下方式注册我的路线:

var App = angular.module('App', ['ngRoute', 'routesControllers']);

App.config(['$routeProvider',
function($routeProvider) {
    $routeProvider.
        when('/', {
            template: " ",
            controller: 'NewOrderCtrl'
        }).
        when('/:companyName/:orderNumber', {
            template: " ",
            controller: 'ViewOrderCtrl'
        }).
        otherwise({
            redirectTo: '/'
        });
}]);

Here, I am not defining anything in the template because I use my routes to fire ajax GETs. 在这里,我没有在模板中定义任何内容,因为我使用我的路由来激活ajax GET。

My controllers are as follows: 我的控制器如下:

var routesControllers = angular.module('routesControllers', []);

routesControllers.controller('NewOrderCtrl', ['OrderManagerFactory', function(OrderManagerFactory) {

  alert('NewOrderCtrl fired!');

  //create a new empty company and order
  OrderManagerFactory.createNewCompany();
  OrderManagerFactory.createNewOrder();

}]);

routesControllers.controller('ViewOrderCtrl', ['OrderManagerFactory', '$routeParams', function(OrderManagerFactory, $routeParams) {

  alert('ViewOrderCtrl fired!');

  //Fire a bunch of ajax calls using the data from $routeParams

}]);

I also put a div for my empty view: 我还为我的空视图添加了一个div:

<div ng-view ng-controller="ViewOrderCtrl"></div>

And when I visit #/tesla/x123x for example, I get nothing. 当我访问#/tesla/x123x ,我什么都没得到。 The alert() in either one of the controllers does not execute... 其中一个控制器中的alert()不执行...

Can somebody help? 有人可以帮忙吗? What am I missing? 我错过了什么?

You could instead use resolve rather than trying to use the controller ... in a way, it's not really a controller you have there. 您可以使用resolve而不是尝试使用controller ...在某种程度上,它不是真正的控制器。

resolve - {Object.=} - An optional map of dependencies which should be injected into the controller. resolve - {Object。=} - 应该注入控制器的可选依赖关系映射。 If any of these dependencies are promises, the router will wait for them all to be resolved or one to be rejected before the controller is instantiated. 如果这些依赖项中的任何一个是promise,路由器将等待它们全部被解析或者在实例化控制器之前被拒绝。 https://docs.angularjs.org/api/ngRoute/provider/ $routeProvider https://docs.angularjs.org/api/ngRoute/provider/ $ routeProvider

So something like: 所以类似于:

 $routeProvider.when('/', { resolve: { init: 'NewOrderService' } });

And change the controller to be factory or service: 并将控制器更改为工厂或服务:

routesControllers.service('NewOrderService', ['OrderManagerFactory', function(OrderManagerFactory) {

  alert('NewOrderService fired!');

  //create a new empty company and order
  OrderManagerFactory.createNewCompany();
  OrderManagerFactory.createNewOrder();

}]);

Edit 编辑

If the route has some parameters, these can be accessed via $routeParams . 如果路由有一些参数,可以通过$routeParams访问这些参数。

It's not a reusable pattern to have the factory/service depend on $routeParams , so updating the resolve property to use a function will help: 工厂/服务依赖于$routeParams不是可重用的模式,因此更新resolve属性以使用函数将有助于:

From the docs again: 再次从文档中:

Be aware that ngRoute.$routeParams will still refer to the previous route within these resolve functions. 请注意, ngRoute.$routeParams仍将引用这些解析函数中的上一个路由。 Use $route.current.params to access the new route parameters, instead $route.current.params使用$route.current.params来访问新的路由参数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM