AngularJS: $viewContentLoaded fired before partial view appears

This is related to angular digest cycle, it’s about how angular works underneath the hood, data binding etc. There are great tutorials explaining this. To solve your problem, use $timeout, it will make the code execute on the next cycle, whem the ng-if was already parsed: app.controller(‘LoginController’, function ($scope, $timeout) { $scope.$on(‘$viewContentLoaded’, function(event) { $timeout(function() … Read more

Angularjs ui-router. How to redirect to login page

The point is, do not redirect if not needed === if already redirected to intended state. There is a working plunker with similar solution .run(function($rootScope, $location, $state, authenticationSvc) { $rootScope.$on( ‘$stateChangeStart’, function(e, toState , toParams , fromState, fromParams) { var isLogin = toState.name === “login”; if(isLogin){ return; // no need to redirect } // now, … Read more

Angular UI-Router How to create a “layout” state?

try this, practically your header and footer are static templates but you can add the controllers in case you need to add some dynamic functionality to it, the header and the footer will be included by default since the route is ”, so try that out: app.config([‘$stateProvider’, function($stateProvider){ $stateProvider .state(‘root’,{ url: ”, abstract: true, views: … Read more

Angular – ui-router get previous state

I use resolve to save the current state data before moving to the new state: angular.module(‘MyModule’) .config([‘$stateProvider’, function ($stateProvider) { $stateProvider .state(‘mystate’, { templateUrl: ‘mytemplate.html’, controller: [“PreviousState”, function (PreviousState) { if (PreviousState.Name == “mystate”) { // … } }], resolve: { PreviousState: [“$state”, function ($state) { var currentStateData = { Name: $state.current.name, Params: angular.copy($state.params), URL: … Read more

Angular-UI Router: Nested Views Not Working

I created working plunker here NOTE: You should read about state nesting and named views more. Because the current state and view definition is simply wrong. Nested States & Nested Views Multiple Named Views Firstly, we should not use the ONE state definition with many views: {}. But we should split them into real states. … Read more

UI-router interfers with $httpbackend unit test, angular js

Take this gist https://gist.github.com/wilsonwc/8358542 angular.module(‘stateMock’,[]); angular.module(‘stateMock’).service(“$state”, function($q){ this.expectedTransitions = []; this.transitionTo = function(stateName){ if(this.expectedTransitions.length > 0){ var expectedState = this.expectedTransitions.shift(); if(expectedState !== stateName){ throw Error(“Expected transition to state: ” + expectedState + ” but transitioned to ” + stateName ); } }else{ throw Error(“No more transitions were expected! Tried to transition to “+ stateName ); … Read more

Angular ui router unit testing (states to urls)

Been having this issue as well, and finally figured out how to do it. Here is a sample state: angular.module(‘myApp’, [‘ui.router’]) .config([‘$stateProvider’, function($stateProvider) { $stateProvider.state(‘myState’, { url: ‘/state/:id’, templateUrl: ‘template.html’, controller: ‘MyCtrl’, resolve: { data: [‘myService’, function(service) { return service.findAll(); }] } }); }]); The unit test below will cover testing the URL w/ params, … Read more