【问题标题】:Angular2 SPA (REST API) basic get (CRUD)Angular2 SPA(REST API)基本获取(CRUD)
【发布时间】:2017-01-29 01:19:39
【问题描述】:

早安,

我的后端运行良好,但无法完成前端!

我对自己配置背面的能力很有信心,但是当我加载所需的页面时,我无法显示我的数据。

有人可以建议或指导教程吗?

这是我目前用来协助的:Angular-2-crud

感谢GWS

cashmovement-list.component.ts

import { Component, OnInit, ViewChild, Input, Output, trigger, state, style, animate, transition } from '@angular/core';

import { ModalDirective } from 'ng2-bootstrap';

import { DataService } from '../shared/services/data.service';
import { DateFormatPipe } from '../shared/pipes/date-format.pipe';
import { ItemsService } from '../shared/utils/items.service';
import { NotificationService } from '../shared/utils/notification.service';
import { ConfigService } from '../shared/utils/config.service';
import { ICashMovement, Pagination, PaginatedResult } from '../shared/interfaces';

@Component({
moduleId: module.id,
selector: 'cashmovements',
templateUrl: 'cashmovement-list.component.html'
})

export class CashMovementListComponent implements OnInit { 
    public cashmovements: ICashMovement[];

constructor(private dataService: DataService,
    private itemsService: ItemsService,
    private notificationService: NotificationService) { }


ngOnInit() {
    this.dataService.getCashMovements()
        .subscribe((cashmovements: ICashMovement[]) => {
            this.cashmovements = cashmovements;
        },
        error => {
            this.notificationService.printErrorMessage('Failed to load users. ' + error);
        });
} 

}

cashmovement-list.component.html

<button class="btn btn-primary" type="button" *ngIf="cashmovements">
   <i class="fa fa-calendar" aria-hidden="true"></i> CashMovements  
   <span class="badge">{{totalItems}}</span>
</button>
 
<hr/>
 
<div [@flyInOut]="'in'">
    <table class="table table-hover">
        <thead>
            <tr>
                <th><i class="fa fa-text-width fa-2x" aria-hidden="true"></i>Cash Movement ID</th>
                <th><i class="fa fa-user fa-2x" aria-hidden="true"></i>PortfolioCode</th>
                <th><i class="fa fa-paragraph fa-2x" aria-hidden="true"></i>CCY Out</th>
                <th><i class="fa fa-map-marker fa-2x" aria-hidden="true"></i>Account Out</th>
                <th><i class="fa fa-calendar-o fa-2x" aria-hidden="true"></i>Date</th>
            </tr>
        </thead>
        <tbody>
            <tr *ngFor="let cashmovement of cashmovements">
                <td> {{cashmovement.CashMovementId}}</td>
                <td>{{cashmovement.PortfolioCode}}</td>
                <td>{{cashmovement.Ccyo}}</td>
                <td>{{cashmovement.AccountO}}</td>
                <td>{{cashmovement.Date | dateFormat | date:'medium'}}</td>
            </tr>
        </tbody>
    </table> 
</div>

interfaces.ts

export interface ICashMovement {
 CashMovementId: number;
 PortfolioCode: string;
 Date: Date;
 Ccyo: string;     
 AccountO: string;
 ValueO: number;
 Ccyi: string;     
 AccountI: string;
 ValueI: number;
 status: string;
 comment: string;
 LastUpdate: number;
}

app.module.ts

import './rxjs-operators';

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule }    from '@angular/http';

import { PaginationModule } from 'ng2-bootstrap/ng2-bootstrap';
import { DatepickerModule } from 'ng2-bootstrap/ng2-bootstrap';
import { Ng2BootstrapModule } from 'ng2-bootstrap/ng2-bootstrap';
import { ModalModule } from 'ng2-bootstrap/ng2-bootstrap';
import { ProgressbarModule } from 'ng2-bootstrap/ng2-bootstrap';
import { SlimLoadingBarService, SlimLoadingBarComponent } from 'ng2-slim-    loading-bar';
import { TimepickerModule } from 'ng2-bootstrap/ng2-bootstrap';

import { AppComponent }   from './app.component';
import { DateFormatPipe } from './shared/pipes/date-format.pipe';
import { HighlightDirective } from   './shared/directives/highlight.directive';
import { HomeComponent } from './home/home.component';
import { MobileHideDirective } from './shared/directives/mobile-hide.directive';

import { CashMovementListComponent } from './cashmovements/cashmovement-list.component';

import { routing } from './app.routes';


import { DataService } from './shared/services/data.service';
import { ConfigService } from './shared/utils/config.service';
import { ItemsService } from './shared/utils/items.service';
import { MappingService } from './shared/utils/mapping.service';
import { NotificationService } from './shared/utils/notification.service';

@NgModule({
imports: [
    BrowserModule,
    DatepickerModule,
    FormsModule,
    HttpModule,
    Ng2BootstrapModule,
    ModalModule,
    ProgressbarModule,
    PaginationModule,
    routing,
    TimepickerModule
],
declarations: [
    AppComponent,
    DateFormatPipe,
    HighlightDirective,
    HomeComponent,
    MobileHideDirective,
    SlimLoadingBarComponent,
    CashMovementListComponent        
],
providers: [
    ConfigService,
    DataService,
    ItemsService,
    MappingService,
    NotificationService,
    SlimLoadingBarService
],
bootstrap: [AppComponent]
})
export class AppModule { }

data.service.ts

import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import {Observer} from 'rxjs/Observer';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

import { ICashMovement, Pagination, PaginatedResult } from '../interfaces';
import { ItemsService } from '../utils/items.service';
import { ConfigService } from '../utils/config.service';

@Injectable()
export class DataService {

    _baseUrl: string = '';

    constructor(private http: Http,
        private itemsService: ItemsService,
        private configService: ConfigService) {
        this._baseUrl = configService.getApiURI();
    }


    getCashMovements(): Observable<ICashMovement[]> {
        return this.http.get(this._baseUrl + 'cashmovements')
            .map((res: Response) => { return res.json();     })
            .catch(this.handleError);
    }

    private handleError(error: any) {
        var applicationError = error.headers.get('Application-Error');
        var serverError = error.json();
        var modelStateErrors: string = '';

        if (!serverError.type) {
            console.log(serverError);
            for (var key in serverError) {
                if (serverError[key])
                    modelStateErrors += serverError[key] + '\n';
            }
        }

        modelStateErrors = modelStateErrors = '' ? null : modelStateErrors;

        return Observable.throw(applicationError || modelStateErrors || 'Server error');
    }


}

【问题讨论】:

  • 你什么也没看到?你得到什么样的输出?你得到了至少th 的表吗?
  • 嗨 Husein,页面正在加载标题

标签: rest asp.net-web-api angular typescript crud


【解决方案1】:

我认为问题可能出在您的templateUrl 上。无论您是否使用moduleId,都需要在局部视图前加上./。你需要指定templateUrl: "./cashmovement-list.component.html"

但是,如果您在浏览器的 dve 控制台中遇到任何错误,您应该将其发布为您的问题的最新消息。

【讨论】:

  • 这是一个很好的选择,谢谢。它还没有完全解决问题我现在得到一个
  • 您使用的是 ASP.NET Web API 吗?会不会是您拥有相同的服务器端 CashMovement 模型,同时您启用了驼峰式套管?您将获得与服务器上一样多的行,但由于从服务器到客户端没有明确的转换,您的属性可能为空。在您的 ngOnInit 中尝试调用 console.log (this.cashmovements) 作为下一行,以便您可以看到来自服务器的内容。
  • 好的,现在展开结果,然后展开其中一个对象以查看属性的名称及其值。如果属性的值为 null(我敢打赌),这可能意味着从服务器到客户端的映射不正确。在您链接到您的问题的文章中,请注意他的export interface IWhateverHeCalledIt 中的属性都是小写字母,而您的属性是大写的。您可能必须更改这些名称并在 HTML 中反映更改。
  • 属性不为空,但确实是混淆了映射
  • 好东西!很高兴我能提供帮助!
猜你喜欢
  • 1970-01-01
  • 2020-07-30
  • 2016-09-30
  • 1970-01-01
  • 2017-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多