angular-promise

Angular2 this is null in component

情到浓时终转凉″ 提交于 2019-12-11 06:02:40
问题 I'm expecting some weird situations where "this" is null inside a component. SO far, I saw it in two situations: 1) When the promise is rejected: if (this.valForm.valid) { this.userService.login(value).then(result => { if(result.success){ this.toasterService.pop("success", "Exito", "Inicio de session correcto"); this.sessionService.setUser(result.data); this.router.navigateByUrl('home'); } else{ this.error = result.code; } },function(error){ console.log("ERROR: " + error); this.error = "ERROR

Angular2 Error while resolving Promise: Type 'Promise<Hero[]>' is no t assignable to type 'Hero[]'

淺唱寂寞╮ 提交于 2019-12-11 05:47:25
问题 Updated. I am Following Angular2 tutorial from https://angular.io/docs/ts/latest/tutorial/toh-pt4.html Here is the call to HeroService from heroes.component.ts, Heroes.component.ts import { Component , OnInit } from '@angular/core'; import { Hero } from './hero'; import { HeroService } from './hero.service'; @Component({ selector: 'my-heroes', template: ` <h2>My Heroes</h2> <ul class="heroes"> <li *ngFor="let hero of heroes"> <span class="badge">{{hero.id}}</span> {{hero.name}} </li> </ul> `,

Chained promises in Angular

廉价感情. 提交于 2019-12-11 05:18:34
问题 I have a list of records I want to process in sequence. A "root" promise needs to be passed back to be added as part of another parent chain. processCategories = function(categories) { var deferred = $q.defer(); var promise = deferred.promise; // Process each category in sequence angular.forEach(categories, function (data, classIndex) { promise = promise.then(doSomethingAndReturnAPromise(categories, data, classIndex)); if (classIndex === categories.length - 1) { // Last category has been

Can this promise nesting be changed to chaining?

怎甘沉沦 提交于 2019-12-11 03:06:15
问题 This is the pseudo scenario | then (items) | then (items, actions) getItems() | getActions(for:items) | apply(actions -> items) :promise | :promise | model <= items | | :synchronous So in words: I need to get a list of global items. Fine. Items fetched. Make a request for actions that user has taken over previously fetched items. Fine. Actions fetched. Apply actions to items list. Put items on the model and display in view. This is somewhat the code I'm using return itemsResource .getItems

Ionic storage “get” returns null only on the second call within method

老子叫甜甜 提交于 2019-12-11 00:32:38
问题 I am facing a very strange issue with Ionic Storage. I have a method that reads a value from the storage and returns an a promise containing an object corresponding to that: private getAuthorizedOptions(): Promise<RequestOptions> { return this._storage.get('bdAccessToken') .then(v => { console.log("access token: ", v); let token = v; let header = new Headers({ 'Authorization': 'Bearer ' + token }); let ro = new RequestOptions({ headers: header }); let options = new RequestOptions(); if

Share data between controllers in AngularJS

此生再无相见时 提交于 2019-12-10 22:08:25
问题 I use the following factory to fetch data from API and store it to local variable called apiData. app.factory('MyFactory', function($resource, $q) { var apiData = []; var service = {}; var resource = $resource('http://example.com/api/sampledata/'); service.getApiData=function() { var itemsDefer=$q.defer(); if(apiData.length >0) { itemsDefer.resolve(apiData); } else { resource.query({},function(data) { apiData=data; itemsDefer.resolve(data) }); } return itemsDefer.promise; }; service.add

$q: default reject handler

大憨熊 提交于 2019-12-10 21:18:23
问题 I want to return a $q instance so that if clients don't call 'then' with a reject handler, then a default one runs. E.g. assume the default is to alert(1) Then mypromise.then(function(result){...}) will alert 1 but mypromise.then(null, function(reason){alert(2)}) will alert 2 回答1: Let's assume there was a way to do that. So we have a magical machine that can always find out if .then is called with a function second argument for a specific promise or not. So what? So you can detect if someone

Is there an easy way to return a success call to $q.all without having to create a $q.defer() variable?

蓝咒 提交于 2019-12-10 19:18:38
问题 My application has a number of synchronous and asynchronous (doing a $http call) methods. In the controllers I have code like this: $q.all([ asyncMethod1(), syncMethod1() ]) .then(function (results) { Even though I have no need to wait on the syncMethod1() I put this inside the $q.all to keep things simple and to allow me to change the method to async if I wanted in the future. For the sync functions I am calling them like this: var syncMethod1 = function () { var defer = $q.defer(); var abc

Angular $http.get: How to catch all the errors?

爱⌒轻易说出口 提交于 2019-12-10 10:22:28
问题 Im sending a form to nodejs for authentication. Using $http.get in the following function and adding a promise > .then . In production, does this handle all the errors that I may get from the server? Do I need to add anything else to this function? MyApp.controller("Login", function($scope, $http){ $scope.checkuser = function(user){ $http.get('/login', user).then(function(response){ if(response.data){ console.log(response.data); //based on response.data create if else .. } else { console.log(

How to wait for a function to finish its execution in angular 2.?

萝らか妹 提交于 2019-12-09 13:21:30
问题 Below is my code, I want login() and authenticated() functions to wait for getProfile() function to finish its execution. I tried several ways like promise etc. but I couldn't implement it. Please suggest me the solution. import { Injectable } from '@angular/core'; import { tokenNotExpired } from 'angular2-jwt'; import { myConfig } from './auth.config'; // Avoid name not found warnings declare var Auth0Lock: any; @Injectable() export class Auth { // Configure Auth0 lock = new Auth0Lock