Ui-sref does not generate interactive links / does not work

I used ng-route for this before, and it worked fine, but with the UI Router interface the links are no longer clickable or, at least in most cases, they are not. When they are, which is very random, they do not display the html templates that I use.

HTML:

<head> <title>Tutorial</title> <script src="lib/angular/angular.js"></script> <script src="lib/angular/angular-ui-router.min.js"></script> <script src="js/app.js"></script> <script src="js/controllers.js"></script> </head> <body> <ng-view></ng-view> <ul class="menu"> <li><a ui-sref="view1">view1</a></li> <li><a ui-sref="view2">view2</a></li> </ul> 

.js

 angular.module('myApp', [ 'myApp.controllers', 'ui.router' ]); angular.module('myApp').config(function($stateProvider, $urlRouterProvider) { $stateProvider.state('view1',{ url: '/view1', controller:'Controller1', templateUrl:'/view1.html' }).state('view2', { url: '/view2/:firstname/:lastname', controller: 'Controller2', resolve: { names: function() { return ['Misko', 'Vojta', 'Brad']; } }, templateUrl: '/view2.html' }); $urlRouterProvider.otherwise('/view1'); }); angular.module('myApp.controllers', []).controller('Controller1', function($scope, $location, $state) { $scope.loadView2=function() { $state.go('view2', { firstname: $scope.firstname, lastname: $scope.lastname }); }; }).controller('Controller2', function($scope, $stateParams, names) { $scope.firstname = $stateParams.firstname; $scope.lastname = $stateParams.lastname; $scope.names = names; }); 

I follow the instructions in the SitePoint book on AngularJS, so I'm not quite sure what I'm doing wrong or what I missed.

http://plnkr.co/edit/amh3nZmyB7lC2IQi3DIn

+6
source share
2 answers

I do not see ui-view at any point in the markup, so your state views are most likely not displayed or entered.

You do not need ng-view . The only ui-view should exist in your main HTML file. Your top-level states will be displayed and entered there. States can have substations, and each state template that has a substation must have its own ui-view , where its rendered substations will be inserted.

Take a look at the ui-router readme to get it working. In addition, they have a good demo application whose source really helped me understand what was going on and how to use it.

+9
source

Useful information, I hope ...

Despite the fact that the above answers are correct (there is no ui-view , referring to ui.router , etc.), I am faced with situations where other problems with loading the view will not lead to the creation of links. For example, this morning I forgot to access the angular-cache module and ran into a problem with CacheFactory not loading (completely unrelated to ui.router ). Until the links were resolved, ui.router was dead in the water.

+2
source

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


All Articles