में वेब एपीआई से डेटा प्राप्त करना "Tour of Heroes" नामक कोणीय 2 पृष्ठ पर ट्यूटोरियल के लिए धन्यवाद, मैं एक साधारण कोणीय 2 एप्लिकेशन बनाने में कामयाब रहा। फिर एनीटीटी फ्रेमवर्क का उपयोग करके मैंने डेटाबेस बनाने का फैसला किया। और नायकों की सूची भरें (फ़ाइल से नहीं)। मैंने वेब एपी कंट्रोलर बनाया और सरल विधि प्राप्त की। फिर hero.service.ts
में मैं नायकों की सूची प्राप्त करने के लिए इस विधि को कॉल करता हूं। जब मैं अपना ऐप लंच करता हूं तो यह नायकों की सूची दिखाता है लेकिन बिना किसी मूल्य के (नाम और आईडी खाली हैं)। जब मैं ब्राउज़र में अपना एप्लिकेशन डीबग करता हूं तो मैं this.heroes
ऑब्जेक्ट heroes.component.ts
में सही डेटा देख सकता हूं। तो क्या चल रहा है? name
और id
क्यों नहीं दिखा रहे हैं?एंगुलर 2
hero.service.ts:
import {Injectable} from 'angular2/core';
import {HEROES} from './mock-heroes';
import {Hero} from './hero';
import {Http, Response} from 'angular2/http';
import 'rxjs/Rx';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class HeroService {
public values: any;
constructor(public _http: Http) { }
private _heroesUrl = 'http://localhost:61553/api/values'; // URL to web api
getHeroes() {
return this._http.get(this._heroesUrl)
.map(res => <Hero[]>res.json())
.catch(this.handleError);
}
private handleError(error: Response) {
console.error(error);
return Observable.throw(error.json().error || 'Server error');
}
}
heroes.component.ts:
import {Component, OnInit} from 'angular2/core';
import {Router} from 'angular2/router';
import {Hero} from './hero';
import {HeroDetailComponent} from './hero-detail.component';
import {HeroService} from './hero.service';
@Component({
selector: 'my-heroes',
templateUrl: 'templates/heroes.component.html',
styleUrls: ['styles/heroes-component.css'],
directives: [HeroDetailComponent]
})
export class HeroesComponent implements OnInit {
constructor(private _heroservice: HeroService, private _router: Router) { }
errorMessage: string;
public heroes: Hero[];
selectedHero: Hero;
ngOnInit() {
this.getHeroes();
}
onSelect(hero: Hero)
{
this.selectedHero = hero;
}
getHeroes() {
this._heroservice.getHeroes()
.subscribe(
value => this.heroes = value,
error => this.errorMessage = <any>error);
}
gotoDetail() {
this._router.navigate(['HeroDetail', { id: this.selectedHero.Id }]);
}
}
heroes.component.html:
<h2>My Heroes</h2>
<ul class="heroes">
<li *ngFor="#hero of heroes" [class.selected]="hero === selectedHero" (click)="onSelect(hero)">
<span class="badge">{{hero.Id}}</span> {{hero.Name}}
</li>
</ul>
<div *ngIf="selectedHero">
<h2>
{{selectedHero.Name | uppercase}} is my hero
</h2>
<button (click)="gotoDetail()">View Details</button>
</div>
hero.ts:
export class Hero {
Id: number;
Name: string;
}
वेब एपीआई नियंत्रक:
using Microsoft.AspNet.Mvc;
using System.Collections.Generic;
using TestApplicationDataAccess;
using TestApplicationDataAccess.Entities;
namespace WebApplication2.Controllers
{
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly TestAppDbContext _dbContext;
public ValuesController(TestAppDbContext dbContext)
{
_dbContext = dbContext;
}
// GET: api/values
[HttpGet]
public IEnumerable<Hero> Get()
{
return _dbContext.Heroes;
}
}
}
हीरो निकाय:
namespace TestApplicationDataAccess.Entities
{
public class Hero
{
public int Id { get; set; }
public string Name { get; set; }
}
}
JSON वेब एपीआई नियंत्रक से लिया गया:
[{"Id":1,"Name":"Superman"}]
क्या आपको कंसोल में उचित प्रतिक्रिया मिलती है? क्या आप कैमेलकेस या कुछ में प्रतिक्रिया बदलते हैं? एचटीएमएल में '{{नायकों | जेसन}} डाल दें और मुझे बताएं कि आपको क्या मिलता है? – micronyks
हाँ कंसोल में यह कहता है स्थिति: ठीक है। और मैं नए टैब में जेसन खोल सकता हूं और उपरोक्त मानों को देख सकता हूं। जब मैं '{{नायकों | जेसन}} डालता हूं 'मुझे कुछ भी नहीं दिख रहा है ... – SteppingRazor
क्या आपको कोई त्रुटि है? – micronyks