Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 从查询字符串中提取数据_Javascript_Angularjs - Fatal编程技术网

Javascript 从查询字符串中提取数据

Javascript 从查询字符串中提取数据,javascript,angularjs,Javascript,Angularjs,我正在尝试从此URL获取数据: chuck是搜索词,可以是其他任何词 我想从数组中获取value属性。邮递员给我这个: { "total": 9735, "result": [ { "category": [ "dev" ], "icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png", "id": "zdj0bfkjsmup6pkb2rpmbw", "url": "http://

我正在尝试从此URL获取数据: chuck是搜索词,可以是其他任何词

我想从数组中获取value属性。邮递员给我这个:

{
"total": 9735,
"result": [
{
  "category": [
    "dev"
  ],
  "icon_url": "https://assets.chucknorris.host/img/avatar/chuck-norris.png",
  "id": "zdj0bfkjsmup6pkb2rpmbw",
  "url": "http://api.chucknorris.io/jokes/zdj0bfkjsmup6pkb2rpmbw",
  "value": "Chuck Norris is the only human being to display the Heisenberg 
  uncertainty principle - you can never know both exactly where and how 
  quickly he will roundhouse-kick you in the face."
 },
我的服务编辑:

import { HttpModule } from '@angular/http';
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import { Joke } from '../joke'

@Injectable()
export class PretragaService {
constructor(private http: Http){}
searchJokes(str: string){return 
this.http.get('https://api.chucknorris.io/jokes/search?query=' + str)
                    .map(res => res.json())
                    .map(joke => {return joke.value});
}

}
组件编辑:

import { Component } from '@angular/core';
import { PretragaService } from '../services/pretraga.service';

@Component({
selector: 'pretraga',
template: `
<input type="text" class="form-control" placeholder="pretraži" 
[(ngModel)]="searchJoke"
name="searchJoke" (keyup)="searchJokes()">
<div *ngIf="searchRes">
<div *ngFor="let joke of searchRes">
<ul>
<li>{{joke}}</li>
</ul>
</div>
</div>
`,
providers: [PretragaService]
})
export class PretragaComponent {
searchJoke: string;
searchRes: any[]=[];

constructor(private searchService: PretragaService){

}
searchJokes(){
   this.searchService.searchJokes(this.searchJoke)
   .subscribe(joke => {
   this.searchRes = joke;
});
}

}

//修改您的myService类:

    searchJokes(str: string){return 
    this.http.get('https://api.chucknorris.io/jokes/search?query=' + str)
                        .map(res => res.json())
                        .map(joke => {return joke.value});
    }
//和组成部分:

    searchJoke: string;
    searchRes: any[]=[];

    constructor(private searchService: PretragaService){

    }
    searchJokes(){
       this.searchService.searchJokes(this.searchJoke)
       .subscribe(joke => {
       this.searchRes = joke;
    });

因此,您将在json对象中获得value属性,还需要什么。?你的问题不清楚!请详细说明。这一切都取决于API,所以只要检查API文档,如果您可以在Url中传递一些条件参数以仅获取值,或者如果它是您的API,则修改它并仅返回值。即使没有,您也可以轻松地修改来自响应的对象,以仅获取值。类似于var myArray=obj.result.mapfunctionres{return res.value}。然后将myArray作为所有检索到的值的数组我可能映射错了,我将在执行代码时编辑问题,错误是:无法读取UnfineDis的“unshift”属性此html有问题吗?{{joke.value}}按照上面的答案所述进行更改后,您的值作为数组位于this.searchRes中。所以直接呈现它,而不是{{joke.value}}它应该是{{joke}}你甚至可以通过在console.logthis.searchResit中打印它来检查this.searchResit中的值。searchResit不呈现,我将它改为{joke},console返回未定义的这是html{joke}控制台返回空数组[]长度:0_u协议:阵列0
    searchJoke: string;
    searchRes: any[]=[];

    constructor(private searchService: PretragaService){

    }
    searchJokes(){
       this.searchService.searchJokes(this.searchJoke)
       .subscribe(joke => {
       this.searchRes = joke;
    });