AngularJS: permission not in RouteProvider, but in the controller?

I saw a few code examples here Undo AngularJS route change before loading the model to prevent flickering

And right now, although this was the right way, I need my LOAD controller to be only when the solution has finished loading, usually most of the examples around will tell you that the code was allowed in the router as a built-in function, but it sounds wrong. The controller needs this, so it is not necessary for the controller to implement this function. It sounded exactly what I was looking for, i.e. Does this seem to use a prototype template?

function PhoneListCtrl($scope, phones) { $scope.phones = phones; $scope.orderProp = 'age'; } PhoneListCtrl.resolve = { phones: function(Phone, $q) { // see: https://groups.google.com/forum/?fromgroups=#!topic/angular/DGf7yyD4Oc4 var deferred = $q.defer(); Phone.query(function(successData) { deferred.resolve(successData); }, function(errorData) { deferred.reject(); // you could optionally pass error data here }); return deferred.promise; } } 

The problem is that I have such a controller

 'use strict'; angular.module('TestApp') .controller('ItemsCtrl', function ($scope) { }); 

So, how do I apply a new function on the controller when my controller is declared inside the module?

I really need something like

  TestCtrl.resolve = { items: function( $q) { .......... return deferred.promise; } } 

This would then allow me to have in my router.

  when('/items', { templateUrl: 'views/items.html', controller: 'TestCtrl', resolve: 'TestCtrl.resolve'}). // Need to use ' around resolve? 

But I'm confused, how could I make this work?

I would really like any feedback, I'm at a loss.

+4
source share
2 answers

It is impossible to define as "TestCtrl.resolve", if you want to use permission with the syntax .controller, then you must define the built-in line in the route provider. The advantage of the built-in solution in the router-router is that you can easily use the controller, but using the same controller and changing the logic in the resolution function

+7
source

You can also use the service:

 resolve: {data : function(service) { return service.getData(); }} 
+4
source

Source: https://habr.com/ru/post/1492062/


All Articles