天天看點

ionic2+angular2

1.資料綁定

首先我們知道每一個界面對應3個檔案構成如圖:

ionic2+angular2

其中html中的寫

然後在ts檔案中對應定義

export class HomePage {  
    public foundRepos;
    public username;

    constructor(private github: GitHubService,  
                private nav: NavController) {
    }
           

就有了angular中資料動态更新的更能

2.HTTP請求

這是基本用法。 然後看下項目中Http請求發送的方法

@Injectable()
export class GitHubService {  
    constructor(private http: Http) {
    }

    getRepos(username) {
        let repos = this.http.get(`https://api.github.com/users/${username}/repos`);
        return repos;
    }

    getDetails(repo) {  
        let headers = new Headers();
        headers.append('Accept','application/vnd.github.VERSION.html');

        return this.http.get(`${repo.url}/readme`, { headers: headers });
    }
}
           

這裡是在service内中,其中注意,typejs中 constructor(private http: Http) {}可能相當與構造方法,定義了一些下面可能要用的屬性,我麼的HTTP類 在這裡申明後才能使用,定義HTTP對象,傳回值轉為JSon,回調處理data。

源碼位址:這裡寫連結内容