如何在角度2的旋转木马滑块中显示Json数据?

时间:2017-08-01 06:06:08

标签: javascript json angular typescript

我从API获取Json数据。我想在Carousel滑块中显示数据。我尽力显示但没有找到结果。我是angular 2.的新用户。我知道用于显示的Interpolation {{}}符号,但我添加了数据以便理解这是我的HTML文件:

<div class="carousel slide media-carousel" id="media2">
                        <div class="carousel-inner">

                            <div class="item active">
                                <div class="row col-md-12" style="padding: 0px;">
                                    <div class="col-md-3 text-center slider_Itm">
                                        <div style="height: 160px; width: 264px; background: linear-gradient(rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2)), rgba(0,0,0,0.2) url('app/images/Assets/mob1.png') no-repeat center;">
                                        </div>
                                        <div class="slider_cntnt" style="margin-left: 0px">
                                            <p class="slider_Prod_name">Iphone</p>
                                                <p class="slider_Prod_city">RS 26,000</p>
                                                <p class="slider_Prod_price">Lahore</p>
                                        </div>
                                    </div>
                                    <div class="col-md-3 text-center slider_Itm">
                                        <div style="height: 160px; width: 264px; background: linear-gradient(rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0.75)), rgba(0,0,0,0.75) url('app/images/Assets/mob2.png') no-repeat center;color: white;font-family: 'Montserrat'; font-weight: 400; font-size: 11px;line-height: 26px;">

                                            <div style="padding-top: 30px;">Qualcomm Snapdragon 801</div>
                                            <div>2.3 GHz Quad-core CPU</div>
                                            <div>5.0" Corning Gorilla Glass</div>
                                            <div>3GB RAM/16GB ROM</div>

                                        </div>
                                        <div class="slider_cntnt" style="margin-left: 0px">
                                            <p class="slider_Prod_name">IPHONE 5S</p>
                                            <p class="slider_Prod_city">RS 26,000</p>
                                            <p class="slider_Prod_price">Lahore</p>
                                        </div>
                                    </div>
                                    <div class="col-md-3 text-center slider_Itm">
                                        <div style="height: 160px; width: 264px; background: linear-gradient(rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2)), rgba(0,0,0,0.2) url('app/images/Assets/mob1.png') no-repeat center;">
                                        </div>
                                        <div class="slider_cntnt" style="margin-left: 0px">
                                            <p class="slider_Prod_name">IPHONE 5S</p>
                                            <p class="slider_Prod_city">RS 26,000</p>
                                            <p class="slider_Prod_price">Lahore</p>
                                        </div>
                                    </div>
                                    <div class="col-md-3 text-center slider_Itm">
                                        <div style="height: 160px; width: 264px; background: linear-gradient(rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2)), rgba(0,0,0,0.2) url('app/images/Assets/mob1.png') no-repeat center;">
                                        </div>
                                        <div class="slider_cntnt" style="margin-left: 0px">
                                            <p class="slider_Prod_name">IPHONE 5S</p>
                                            <p class="slider_Prod_city">RS 26,000</p>
                                            <p class="slider_Prod_price">Lahore</p>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <div class="item">
                                <div class="row col-md-12" style="padding: 0px;">
                                    <div class="col-md-3 text-center slider_Itm">
                                        <div style="height: 160px; width: 264px; background: linear-gradient(rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2)), rgba(0,0,0,0.2) url('app/images/Assets/mob1.png') no-repeat center;">
                                        </div>
                                        <div class="slider_cntnt" style="margin-left: 0px">
                                            <p class="slider_Prod_name">IPHONE 5S</p>
                                            <p class="slider_Prod_city">RS 26,000</p>
                                            <p class="slider_Prod_price">Lahore</p>
                                        </div>
                                    </div>
                                    <div class="col-md-3 text-center slider_Itm">
                                        <div style="height: 160px; width: 264px; background: linear-gradient(rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0.75)), rgba(0,0,0,0.75) url('app/images/Assets/mob2.png') no-repeat center;color: white;font-family: 'Montserrat'; font-weight: 400; font-size: 11px;line-height: 26px;">

                                            <div style="padding-top: 30px;">Qualcomm Snapdragon 801</div>
                                            <div>2.3 GHz Quad-core CPU</div>
                                            <div>5.0" Corning Gorilla Glass</div>
                                            <div>3GB RAM/16GB ROM</div>

                                        </div>
                                        <div class="slider_cntnt" style="margin-left: 0px">
                                            <p class="slider_Prod_name">IPHONE 5S</p>
                                            <p class="slider_Prod_city">RS 26,000</p>
                                            <p class="slider_Prod_price">Lahore</p>
                                        </div>
                                    </div>
                                    <div class="col-md-3 text-center slider_Itm">
                                        <div style="height: 160px; width: 264px; background: linear-gradient(rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2)), rgba(0,0,0,0.2) url('app/images/Assets/mob1.png') no-repeat center;">
                                        </div>
                                        <div class="slider_cntnt" style="margin-left: 0px">
                                            <p class="slider_Prod_name">IPHONE 5S</p>
                                            <p class="slider_Prod_city">RS 26,000</p>
                                            <p class="slider_Prod_price">Lahore</p>
                                        </div>
                                    </div>
                                    <div class="col-md-3 text-center slider_Itm">
                                        <div style="height: 160px; width: 264px; background: linear-gradient(rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2)), rgba(0,0,0,0.2) url('app/images/Assets/mob1.png') no-repeat center;">
                                        </div>
                                        <div class="slider_cntnt" style="margin-left: 0px">
                                            <p class="slider_Prod_name">IPHONE 5S</p>
                                            <p class="slider_Prod_city">RS 26,000</p>
                                            <p class="slider_Prod_price">Lahore</p>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </div>
                        <a data-slide="prev" href="#media2" class="left carousel-control" style="background: transparent; color: #1b99e3; opacity: 1;"><span class="glyphicon glyphicon-chevron-left" style="top: 10px; left: 11px;"></span></a>
                        <a data-slide="next" href="#media2" class="right carousel-control" style="background: transparent; color: #1b99e3; opacity: 1;"><span class="glyphicon glyphicon-chevron-right" style="top: 10px; left: 0px;"></span></a>
                    </div>



{
"error": "TheErrorData",
"body": "helloworld",
"data": [{
        "Brand_id": 0,
        "Brand_name": "Samsung",
        "id":0
    },
    {
        "Brand_id": 1,
        "Brand_name": "QMobile",
        "id":1
    },
    {
        "Brand_id": 2,
        "Brand_name": "Nokia",
        "id":0
    },
    {
        "Brand_id": 3,
        "Brand_name": "Huawei",
        "id":1
    },
    {
        "Brand_id": 4,
        "Brand_name": "Acer",
        "id":0
    }
]

}

在脚本文件类型中。

indexmobileshelf = [];

constructor(private _employeelist : SubServices){
}
ngOnInit()
{  
    this._employeelist.MobileShelf()         
    .subscribe(data => this.indexmobileshelf = data);  
}

我添加了文件..一个servive是从我的类型脚本文件中调用的,其中有一个namr MobileShelf()返回 indexmobileshelf 中的Json数据,然后我使用这些数据显示在轮播滑块中。请帮助我

1 个答案:

答案 0 :(得分:0)

你可以为所有重复元素使用循环* ngFor

假设您要显示数组的项目。 你的代码有点混乱阅读。但你可以理解这个概念并相应地应用

<div class="col-md-3 text-center slider_Itm" *ngFor="let mobile of indexmobileshelf">
   <div style="height: 160px; width: 264px; background: linear-gradient(rgba(0, 0, 0, 0.2), rgba(0, 0, 0, 0.2)), rgba(0,0,0,0.2) url('app/images/Assets/mob1.png') no-repeat center;"></div>
        <div class="slider_cntnt" style="margin-left: >
           <p class="slider_Prod_name">{{mobile.Brand_id}}
           <p class="slider_Prod_city">{{mobile.Brand_name}}</p>
           <p class="slider_Prod_price">{{mobile.somethingelse}}</p>
       </div>
   </div>
</div>