如何在AngularJS中阻止http请求,以便我可以在下一行使用$ http响应?
在下面的示例中,$httpobject不会将结果返回到下一行,以便我可以将此结果传递fullcalender()给JavaScript库,因为它$scope.data返回空值.
这是示例代码:
$http.get('URL').success(function(data){
$scope.data = data;
});
$.fullCalender({
data: $scope.data
});
Run Code Online (Sandbox Code Playgroud) 当我在我的服务的构造函数中使用Http时,我面临上述错误.
员工服务代码
import { Component } from '@angular/core'
import { Injectable } from '@angular/core'
import { HttpModule } from '@angular/http';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/observable';
import 'rxjs/add/operator/map';
@Injectable()
export class employeService {
constructor(public http: Http){ }
getEmployeename(): Observable<string> {
return this.http.get('http://localhost:53656/api/values/Getdata').map((resp: Response) =>
resp.json()
);
}
}
Run Code Online (Sandbox Code Playgroud)
注意我在员工组件内部直接调用我的服务(不是来自app.module.ts文件)
员工组件代码
import { Component, OnInit } from '@angular/core';
import { employeService } from './Service/service';
@Component({
templateUrl: './app.employee.html',
providers: [employeService]
})
export class EmployeeComponent implements …Run Code Online (Sandbox Code Playgroud)