AngularJS routing keeps the redirection back to itself, as it does in a loop

I have

<!DOCTYPE html> <html lang="en" data-ng-app="myCustomApp"> <body> <div id="body"> <div ng-view></div> </div> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js" type="text/javascript"></script> <script src="/Js/app.js"></script> <script src="/Js/controllers/controllers.js"></script> </body> </html> 

With the following

 app.controller('SearchController', function ($scope) { init(); function init() { alert("called"); } }); 

and the application is indicated as follows:

 var app = angular.module('myCustomApp', []); app.config(function($routeProvider) { $routeProvider .when('/search', { controller: 'SearchController', templateUrl: '/js/partials/Search.html' }) .otherwise({ redirectTo: '/search' }); }); 

The problem is that the page, while viewing the following content, continues to refresh the page, as in a loop. Any ideas?

/#/Search

+6
source share
1 answer
  var app = angular.module('myCustomApp', ['ngRoute']); 

Routing is not native in angular, you need to add it as a module.

https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular-route.js

This is the CDN for the script for him. I really suggest looking at ui-router, it works very similarly, but uses state or states to control the view or sections of the view.

https://github.com/angular-ui/ui-router

The documentation is really good, and there are some great examples.

+1
source

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


All Articles