scope and controller instantiation with ui router

To get even more detailed answers, we can/should observe the source code and check the documentation. Let me try to explain all three questions (and also cite from code and doc). 1. When do controllers get instantiated? Here we can observe the code of the ui-view directive: [$ViewDirective.$inject = \[‘$state’, ‘$injector’, ‘$uiViewScroll’, ‘$interpolate’\];][1] Controllers are … Read more

AngularJS Dynamic loading a controller

Re-reading this article http://ify.io/lazy-loading-in-angularjs/ suggested to keep a $contentProvider instance inside Angular App. I came up with this code in my app.js demoApp.config(function ($controllerProvider) { demoApp.controller = $controllerProvider.register; }); It enables me to write my controller as expected in a external javascript file : angular.module(“demoApp”).controller(‘MouseTestCtrlA’, fn); Hope this can help !

angularjs getting previous route path

This alternative also provides a back function. The template: <a ng-click=’back()’>Back</a> The module: myModule.run(function ($rootScope, $location) { var history = []; $rootScope.$on(‘$routeChangeSuccess’, function() { history.push($location.$$path); }); $rootScope.back = function () { var prevUrl = history.length > 1 ? history.splice(-2)[0] : “https://stackoverflow.com/”; $location.path(prevUrl); }; });

How/when to use ng-click to call a route?

Routes monitor the $location service and respond to changes in URL (typically through the hash). To “activate” a route, you simply change the URL. The easiest way to do that is with anchor tags. <a href=”#/home”>Go Home</a> <a href=”#/about”>Go to About</a> Nothing more complicated is needed. If, however, you must do this from code, the … Read more

How would I have ui-router go to an external link, such as google.com?

Angular-ui-router doesn’t support external URL, you need redirect the user using either $location.url() or $window.open() I would suggest you to use $window.open(‘http://www.google.com’, ‘_self’) which will open URL on the same page. Update You can also customize ui-router by adding parameter external, it can be true/false. $stateProvider .state(‘external’, { url: ‘http://www.google.com’, external: true }) Then configure … 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

What is the difference between $routeProvider and $stateProvider?

Both do the same work as they are used for routing purposes in SPA(Single Page Application). 1. Angular Routing – per $routeProvider docs URLs to controllers and views (HTML partials). It watches $location.url() and tries to map the path to an existing route definition. HTML <div ng-view></div> Above tag will render the template from the … Read more