Correct way Provide DomSanitizer to Component with Angular 2 RC6

You don’t need to declare providers: [ DomSanitizer ] anymore. Just need to import DomSanitizer as shown below, import { DomSanitizer, SafeResourceUrl, SafeUrl} from ‘@angular/platform-browser’; in component inject it through a constructor as below, constructor(private sanitizer: DomSanitizer) {}

Angular: ‘Cannot find a differ supporting object ‘[object Object]’ of type ‘object’. NgFor only supports binding to Iterables such as Arrays’

As the error messages stated, ngFor only supports Iterables such as Array, so you cannot use it for Object. change private extractData(res: Response) { let body = <Afdelingen[]>res.json(); return body || {}; // here you are return an object } to private extractData(res: Response) { let body = <Afdelingen[]>res.json().afdelingen; // return array from json file … Read more

The view is not updated in AngularJS

You are missing $scope.$apply(). Whenever you touch anything from outside of the Angular world, you need to call $apply, to notify Angular. That might be from: xhr callback (handled by $http service) setTimeout callback (handled by $defer service) DOM Event callback (handled by directives) In your case, do something like this: // inject $rootScope and … Read more

AngularJS – UI Router – programmatically add states

See -edit- for updated information Normally states are added to the $stateProvider during the config phase. If you want to add states at runtime, you’ll need to keep a reference to the $stateProvider around. This code is untested, but should do what you want. It creates a service called runtimeStates. You can inject it into … Read more

use $http inside custom provider in app config, angular.js

The bottom line is: You CANNOT inject a service into the provider configuration section. You CAN inject a service into the section which initializes the provider’s service. Details: Angular framework has a 2 phase initialization process: PHASE 1: Config During the config phase all of the providers are initialized and all of the config sections … Read more

Angular 2: Two backend service calls on success of first service

You need to leverage the flatMap operator to call one request after the previous one completed: this.service.getUserInterests().flatMap( (interests) => { let params: URLSearchParams = new URLSearchParams(); params.set(‘access_token’, localStorage.getItem(‘access_token’)); return this.http.get(‘http://localhost:8080/user/selections’, { search: params }).map((res: Response) => res.json()); } ); When subscribing to this data flow, you will only receive the result of the last request. … Read more

AngularJS : How to watch service variables?

You can always use the good old observer pattern if you want to avoid the tyranny and overhead of $watch. In the service: factory(‘aService’, function() { var observerCallbacks = []; //register an observer this.registerObserverCallback = function(callback){ observerCallbacks.push(callback); }; //call this when you know ‘foo’ has been changed var notifyObservers = function(){ angular.forEach(observerCallbacks, function(callback){ callback(); }); … Read more

angular.service vs angular.factory

angular.service(‘myService’, myServiceFunction); angular.factory(‘myFactory’, myFactoryFunction); I had trouble wrapping my head around this concept until I put it to myself this way: Service: the function that you write will be new-ed: myInjectedService <—- new myServiceFunction() Factory: the function (constructor) that you write will be invoked: myInjectedFactory <— myFactoryFunction() What you do with that is up to … Read more

Passing data between controllers in Angular JS?

I don’t know if it will help anyone, but based on Charx (thanks!) answer I have created simple cache service. Feel free to use, remix and share: angular.service(‘cache’, function() { var _cache, _store, _get, _set, _clear; _cache = {}; _store = function(data) { angular.merge(_cache, data); }; _set = function(data) { _cache = angular.extend({}, data); }; … Read more