When to use the AngularJS `$onInit` Life-Cycle Hook

Code has to be moved in the $onInit function, when it depends on bindings, because these bindings are not available within this in the constructor. They get assigned AFTER instantiation of the component class. Example: You have a state definition like this: $stateProvider.state(“app”, { url: “https://stackoverflow.com/”, views: { “indexView”: { component: “category” } }, resolve: … Read more

How can you limit the value from input using AngularJS?

Can always make a directive for it: app.directive(“limitTo”, [function() { return { restrict: “A”, link: function(scope, elem, attrs) { var limit = parseInt(attrs.limitTo); angular.element(elem).on(“keypress”, function(e) { if (this.value.length == limit) e.preventDefault(); }); } } }]); <input limit-to=”4″ type=”number” class=”form-control input-lg” ng-model=”search.main” placeholder=”enter first 4 digits: 09XX”>

How to wait till the response comes from the $http request, in angularjs?

You should use promises for async operations where you don’t know when it will be completed. A promise “represents an operation that hasn’t completed yet, but is expected in the future.” (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise) An example implementation would be like: myApp.factory(‘myService’, function($http) { var getData = function() { // Angular $http() and then() both return promises themselves … Read more

How to set a global http timeout in AngularJs

This is possible with bleeding-edge angular.js (tested with git master 4ae46814ff). You can use request http interceptor. Like this. angular.module(‘yourapp’) .factory(‘timeoutHttpIntercept’, function ($rootScope, $q) { return { ‘request’: function(config) { config.timeout = 10000; return config; } }; }); And then in .config inject $httpProvider and do this: $httpProvider.interceptors.push(‘timeoutHttpIntercept’);

how to download file using AngularJS and calling MVC API?

I had the same problem. Solved it by using a javascript library called FileSaver Just call saveAs(file, ‘filename’); Full http post request: $http.post(‘apiUrl’, myObject, { responseType: ‘arraybuffer’ }) .success(function(data) { var file = new Blob([data], { type: ‘application/pdf’ }); saveAs(file, ‘filename.pdf’); });

How do I create an AngularJS UI bootstrap popover with HTML content?

UPDATE: As can been seen in this, you should now be able to do this without overriding the default template. ORIGINAL: As of angular 1.2+ ng-bind-html-unsafe has been removed. You should be using the $sce service Reference. Here is a filter for creating trusted HTML. MyApp.filter(‘unsafe’, [‘$sce’, function ($sce) { return function (val) { return … Read more