से गतिशील रूप से एमडी-कार्ड बनाएं I मैं एक एपीआई कॉल कर रहा हूं और उपयोगकर्ता प्रोफाइल का एक समूह संग्रहीत कर रहा हूं, और मैं प्रत्येक प्रोफ़ाइल के लिए गतिशील रूप से कार्ड (कोणीय सामग्री डिजाइन एमडी कार्ड) बनाने में सक्षम होना चाहता हूं। लौटाई गई प्रोफ़ाइल की संख्या भिन्न हो सकती है, इसलिए इसे गतिशील होना चाहिए।एपीआई प्रतिक्रिया
यह जो JSONP अनुरोध करता है और profiles
चर में संग्रहीत करता है प्रोफाइल मेरी घटक फ़ाइल है:
import {Component, Injectable, OnInit} from '@angular/core';
import {Jsonp} from '@angular/http';
@Component({
selector: 'app-staff',
templateUrl: './staff.component.html',
styleUrls: ['./staff.component.css']
})
@Injectable()
export class StaffComponent implements OnInit {
public searchField: string;
apiRoot = 'this/is/my/api/url';
public results: JSON;
public profiles;
constructor(private jsonp: Jsonp) {
}
ngOnInit() {
}
setSearchField(field: string){ // ignore this method
this.searchField = field;
console.log('Received field: ' + this.searchField);
}
search(term: string) {
const apiUrl = `${this.apiRoot}?search=${term}&rows=10&callback=JSONP_CALLBACK`;
return this.jsonp.request(apiUrl).map(results => { this.results = results.json(); console.log(this.results['profiles'][0]); this.profiles = results['profiles']; return results.json(); });
}
}
यह ऊपर घटक है, जहां मैं *ngFor
उपयोग करने के लिए एक बनाने के लिए कोशिश कर रहा हूँ के लिए टेम्पलेट है md-card
की सूची:
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-md-auto">
<ul>
<li *ngFor="let profile of profiles">
<md-card class="example-card">
<md-card-header>
<div md-card-avatar class="example-header-image"></div>
<md-card-title>{{profile.fullName}}</md-card-title>
<md-card-subtitle>Department</md-card-subtitle>
</md-card-header>
<img md-card-image src="../assets/image.png">
<md-card-content>
<p>
This section of the card will contain information about the result being searched for. It could also be
accompanied by additional information such as links.
</p>
</md-card-content>
<md-card-actions>
<button md-button>APPLY</button>
<button md-button>GO TO xyz</button>
</md-card-actions>
</md-card>
</li>
</ul>
</div>
</div>
</div>
मेरे प्रोफाइल डेटा एक सरणी के रूप में है (मान लेते हैं कि सरणी 10 की लंबाई से अधिक नहीं है) और निम्नलिखित रूप लेता है:
0: {fullName: "Foo Bar", emailAddress: "[email protected]", image: "/profile/image/foobar/foobar.jpg", phoneNumber: "99999999"},
1: {fullName: "Foo Bar1", emailAddress: "[email protected]", image: "/profile/image/foobar1/foobar1.jpg", phoneNumber: "919999999"}
हालांकि, md-card
को प्रस्तुत नहीं किया जा रहा है। मैंने जांच की है कि profiles
खाली नहीं है। मैं प्रोफ़ाइल की संख्या के आधार पर गतिशील रूप से कार्ड कैसे बना सकता हूं और प्रोफ़ाइल ऑब्जेक्ट्स से मूल्यों के साथ सामग्री को पॉप्युलेट कर सकता हूं?
आपके 'प्रोफाइल' डेटा कैसा दिखता है?कैब आप इसे अपने प्रश्न में जोड़ते हैं? – Faisal
@ फैसल मैंने अपने प्रश्न में 'प्रोफ़ाइल' डेटा के बारे में अधिक जानकारी दी है। – bawse
'this.profiles = परिणाम ['प्रोफाइल']' होना चाहिए .profiles = this.results ['profiles'] 'या 'this.profiles = results.json() [' प्रोफाइल ']' – cyrix