小编eko*_*eko的帖子

如何从角度中的Observable/http/async调用返回响应?

我有一个服务,它返回一个observable,它向我的服务器发送一个http请求并获取数据.我想使用这些数据,但我总是得到undefined.有什么问题?

服务:

@Injectable()
export class EventService {

  constructor(private http: Http) { }

  getEventList(): Observable<any>{
    let headers = new Headers({ 'Content-Type': 'application/json' });
    let options = new RequestOptions({ headers: headers });

    return this.http.get("http://localhost:9999/events/get", options)
                .map((res)=> res.json())
                .catch((err)=> err)
  }
}
Run Code Online (Sandbox Code Playgroud)

零件:

@Component({...})
export class EventComponent {

  myEvents: any;

  constructor( private es: EventService ) { }

  ngOnInit(){
    this.es.getEventList()
        .subscribe((response)=>{
            this.myEvents = response;
        });

    console.log(this.myEvents); //This prints undefined!
  }
}
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous observable typescript angular

92
推荐指数
3
解决办法
2万
查看次数

rxjs 7 更新 - 主题 - 预期 1 个参数,但得到 0 个

我将 rxjs 从版本 6.xx 更新到 7.xx,但出现以下错误:

src/app/app.component.ts 中出现错误 (12:19) 需要 1 个参数,但得到 0 个。

当尝试将next空值传递给Subject

destroy$ = new Subject();

constructor() {
  this.destroy$.next(); // <-- Expected 1 arguments, but got 0.
}
Run Code Online (Sandbox Code Playgroud)

错误堆栈闪电战

rxjs typescript angular

53
推荐指数
1
解决办法
2万
查看次数

如何动态创建自举模式作为Angular2组件?

原始标题:无法在Angular 2中初始化动态追加(HTML)组件

我已经创建了一个指令,在初始化时将一个模态附加到正文.当单击一个按钮(带有指令注入其中)时,这个模态会激活.但我希望这个模态的内容是另一个组件(实际上我希望模态是组件).似乎我无法初始化组件.

这是我所做的一切:

http://plnkr.co/edit/vEFCnVjGvMiJqb2Meprr?p=preview

我正在尝试将my-comp作为我的组件的模板

 '<div class="modal-body" #theBody>' 
            + '<my-comp></my-comp>' + 
 '</div>
Run Code Online (Sandbox Code Playgroud)

angular

16
推荐指数
1
解决办法
1万
查看次数

Angular 2有条件地在组件中注入服务

例如:

我有2项服务

1)one.service.ts

2)two.service.ts

我有一个组件 - dashboard.component.ts

import { Component, ViewEncapsulation, Inject } from '@angular/core';
import { OneService } from '../services/one.service';
import { TwoService } from '../services/two.service';

@Component({
  selector: 'dashboard',
  encapsulation: ViewEncapsulation.Emulated,
  styleUrls: ['./dashboard.less'],
  templateUrl: './dashboard.html'
})
export class DashboardComponent {
    constructor() {
       // Here how do I get service instance based on some this condition.
         if(true) {
             /* Service **one.service.ts** to be injected */
         } else {
             /* Service **two.service.ts** to be injected */    
         }

    }
}
Run Code Online (Sandbox Code Playgroud)

javascript dependency-injection angular2-services angular

13
推荐指数
1
解决办法
5681
查看次数

VS代码:具有相同名称和不同扩展名的组文件

我正在使用vscode在angular2项目中工作.有没有办法将具有相同名称和不同扩展名的文件分组,就像Visual Studio一样.

例:

  • myscript.ts
  • myscript.js
  • myscript.map.js

typescript visual-studio-code angular

10
推荐指数
1
解决办法
931
查看次数

错误:类型'Hero []'上不存在属性'then'

app.component.ts文件

import { Component, OnInit } from '@angular/core';
import { Hero } from './hero';
import { HeroService } from './hero.service';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css'],
  providers: [HeroService]
})

export class AppComponent implements OnInit{
  title = 'Tour of Heroes';
  heroes : Hero[ ];
  selectedHero : Hero;
  constructor(private heroService: HeroService) { }
      getHeroes(): void {
     this.heroService.getHeroes().then(heroes => this.heroes = heroes);
  };
    ngOnInit(): void {
    this.getHeroes();
  };
    onSelect(hero: Hero): void{
    this.selectedHero = hero;
  };
}
Run Code Online (Sandbox Code Playgroud)

hero.service.ts

import { Injectable } from …
Run Code Online (Sandbox Code Playgroud)

angularjs typescript angular

10
推荐指数
2
解决办法
1万
查看次数

如何从jquery datepicker获取所选日期

我正在使用jquery datepicker来显示一个calendar.Now根据我的要求我想在我的jquery变量中获取用户选择的日期,我将在我的应用程序中使用但我无法获取日期..这是datepciker的代码

<div id="datepicker"></div>
Run Code Online (Sandbox Code Playgroud)

在这里,我试图获取所选代码..

$(document).ready(function () {

        $("#datepicker").datepicker({
            onSelect: function (dateText, inst) {
                var date = $(this).val();
                alert(date);
            }
        });

    });
Run Code Online (Sandbox Code Playgroud)

但是,我无法得到约会..请帮帮我..谢谢..

javascript jquery datepicker

7
推荐指数
2
解决办法
8万
查看次数

如何用2个参数创建主题(rx.js)?

我正在使用subject作为我的angular2应用程序中组件的通信服务的服务.

通话方式:

this.notification.create('test');
Run Code Online (Sandbox Code Playgroud)

服务:

export class NotificationService {
  private static notificationSource = new Subject<any>()

  notiCreated$ = NotificationService.notificationSource.asObservable();

  create(message) {
    NotificationService.notificationSource.next(message);
  }
}
Run Code Online (Sandbox Code Playgroud)

被叫功能:

  this.subscription = notification.notiCreated$.subscribe(
    data => {
       console.log(data);
       this.createNotification(data, 'warning');
  });
Run Code Online (Sandbox Code Playgroud)

但我想通过2个参数.我有错误.

我该怎么做?

subject rxjs typescript angular2-components angular

7
推荐指数
1
解决办法
6689
查看次数

“日期”类型的参数不能分配给“字符串”类型的参数

我正在Angular2-Meteor项目上,正在得到这个

“日期”类型的参数不能分配给“字符串”类型的参数。

当我编写此函数以设置日期值时出错。

start: startOfDay(new Date(attendance.dayAt)),
Run Code Online (Sandbox Code Playgroud)

我尝试将类型设置为日期,但无法正常工作,如何解决此错误?

meteor angular2-meteor angular

7
推荐指数
1
解决办法
2万
查看次数

在Angular 2/TypeScript中使用Google API

之前的一个问题让我得到了大部分工作(希望如此),但是我似乎无法从TypeScript中获得Google API.我基本上遵循这个示例:https://developers.google.com/api-client-library/javascript/samples/samples

我没有看到错误,运行API调用的方法不会被触发.

我已经安装了gapi和gapi.auth的类型.initClient()不会抛出任何错误,它似乎永远不会完成.当前用户是以Google用户身份登录的,但尚未获得API调用授权.这是我要处理的下一件事,但现在这个电话甚至都没有.如下所示,我在方法的开头添加了一个未被调用的日志行.

initGapiClient() {
    gapi.load('client:auth2', this.initClient);
  }

  initClient() {
    gapi.client.init({
      apiKey: '',
      discoveryDocs: ['https://www.googleapis.com/discovery/v1/apis/youtube/v3/rest'],
      clientId: 'xxxxxx.apps.googleusercontent.com',
      scope: 'https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.readonly'
    }).then(function () {
      // Listen for sign-in state changes.
    gapi.auth2.getAuthInstance().isSignedIn.listen(this.updateSigninStatus);

      // Handle the initial sign-in state.
      this.updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
    });
  }

  updateSigninStatus(isSignedIn) {
    if (isSignedIn) {
      this.listPlaylists();
    } else {
      alert("Can't sign in");          
    }
  }

  listPlaylists() {
    console.log("Called listPlaylists");
    ... 
    API call here
    ... 
  }
Run Code Online (Sandbox Code Playgroud)

javascript typescript youtube-data-api angular

7
推荐指数
2
解决办法
6803
查看次数