小编ped*_*nez的帖子

Angular 5 Service读取本地.json文件

我正在使用Angular 5,我使用angular-cli创建了一个服务

我想要做的是创建一个服务,读取Angular 5的本地json文件.

这就是我的......我有点卡住......

import { Injectable } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';

@Injectable()
export class AppSettingsService {

  constructor(private http: HttpClientModule) {
    var obj;
    this.getJSON().subscribe(data => obj=data, error => console.log(error));
  }

  public getJSON(): Observable<any> {
    return this.http.get("./assets/mydata.json")
      .map((res:any) => res.json())
      .catch((error:any) => console.log(error));

  }

}
Run Code Online (Sandbox Code Playgroud)

我怎么能完成这个?

javascript json angular

78
推荐指数
7
解决办法
12万
查看次数

Angular 5检查变量on和*ngIf语句的值

我正在使用angular 5,我正在尝试检查组件的html模板中变量的值.

所以它看起来像这样:

<div *ngIf="item='somevalue'">
Run Code Online (Sandbox Code Playgroud)

我收到这个错误:

ht错误:模板解析错误:

Parser Error: Bindings cannot contain assignments at column 17 in...
Run Code Online (Sandbox Code Playgroud)

这不能在角度上完成吗?

typescript angular

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

Angular 5 Adding a new instance of a component

I am using angular 5 and I want to create new instances of a component on demand.

This is the code I currently have:

app.component.ts:

import { Component } from '@angular/core';
import { MyComponent } from './mycomp/my.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent {
  constructor() {}
  addCmp() {
    console.log('adding');
    // Add an instance of MyComponent code goes here ?
  }
}
Run Code Online (Sandbox Code Playgroud)

app.component.html

<button (click)="addCmp()" >Add New MyComponent below</button>
Run Code Online (Sandbox Code Playgroud)

MyComponent.html:

<div>I am MyComponent</div>
Run Code Online (Sandbox Code Playgroud)

How can I …

javascript angular

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

jQuery CSS到纯Javascript

我试图尽可能少地使用jQuery,我想将一些jQuery代码翻译成纯JS.

我有这个:

$(".myDiv").css({"width": 500});
Run Code Online (Sandbox Code Playgroud)

什么是上面代码的纯JS等价物?

javascript jquery

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

角5 httpClient和承诺

我一直在看Angular 5的GET POST等:

get() {
    return this.httpClient.get<any>('https://api.github.com/users/seeschweiler');
}
Run Code Online (Sandbox Code Playgroud)

要么

http.get<ItemsResponse>('/api/items')
    .subscribe(
       // Successful responses call the first callback.
       data => {...},
       // Errors will call this callback instead:
       err => {
         console.log('Something went wrong!');
       });
Run Code Online (Sandbox Code Playgroud)

我不认为通常会使用诺言。

是因为不是真的需要它还是其他原因?

javascript typescript angular

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

标签 统计

angular ×4

javascript ×4

typescript ×2

jquery ×1

json ×1