2017-12-23 3 views
2

휴대 전화를 평가하는 코드를 작성했습니다. 그래서 HTML과 CSS 파일을 만들었습니다. 나는 4 점과 5 점으로 등급을 매겼지만, 5 점만으로 점수를 표시했다. 디버깅 할 때 입력을 "4"로 표시하지만 "5"로 표시하면 별이 두 줄에 걸쳐 표시됩니다.Typescript 등급 구성 요소가 올바르게 표시되지 않음

문제를 보여주는 이미지 :

내가 여기 내 코드를 부착하고있다. 도와주세요. 사전에 어떤 도움

평가 component.html

//**rating.component.ts** 
 

 

 
    import { 
 
     Component, 
 
     OnChanges, 
 
     Input 
 
    } from '@angular/core'; 
 

 
    @Component({ 
 
     selector: 'app-rating', 
 
     templateUrl: './rating.component.html', 
 
     styleUrls: ['./rating.component.css'] 
 
    }) 
 
    export class RatingComponent implements OnChanges { 
 
     @Input() rating: number; 
 

 
     starWidth: number; 
 
     constructor() { 
 
     console.log("star has created"); 
 
     } 
 

 
     ngOnChanges() { 
 
     console.log("star dynamically added"); 
 
     this.starWidth = this.rating * (86/5); 
 
     } 
 

 
    } 
 
//mobile.component.ts 
 
import { 
 
    Component, 
 
    OnInit 
 
} from '@angular/core'; 
 
import { 
 
    mobile 
 
} from './mobile' 
 

 
@Component({ 
 
    selector: 'app-mobiles', 
 
    templateUrl: './mobiles.component.html', 
 
    styleUrls: ['./mobiles.component.css'] 
 
}) 
 
export class MobilesComponent implements OnInit { 
 
    title: string = 'MOBILE CART'; 
 
    image: string = 'assets/images/'; 
 
    click: boolean = false; 
 
    showImages: boolean = true; 
 
    refineMobile: string = " "; 
 
    shop = 'assets/images/Cart.jpg'; 
 
    mobiles: mobile[] = [{ 
 
     //imageUrl: 'assets/download.jpg', 
 
     imageUrl: 'download.jpg', 
 
     mobile_name: 'Lenovo', 
 
     mobile_price: 10000, 
 
     mobile_code: 'K3', 
 
     release_date: 'mar 19,2016', 
 
     rating: 4 
 
    }, 
 
    { 
 
     //imageUrl: 'assets/download (1).jpg', 
 
     imageUrl: 'download (1).jpg', 
 
     mobile_name: 'samsung', 
 
     mobile_price: 7000, 
 
     mobile_code: 'ON-5', 
 
     release_date: 'nov 18,2017', 
 
     rating: 5 
 
    } 
 
    ]; 
 
    displayImages(): void { 
 
    (this.showImages == true) ? this.showImages = false: this.showImages = true; 
 
    } 
 
    cart: number = 0; 
 
    //adding items in cart 
 
    inCart() { 
 
    console.log("item are added in the cart"); 
 
    return this.cart++; 
 
    } 
 
    //clearing the cart 
 
    clear() { 
 
    return this.cart = 0; 
 
    } 
 

 
    ngOnInit(): void { 
 
    console.log('oninit of angular is initialised'); 
 
    } 
 

 
    //filter by name 
 
    filterMobiles: mobile[]; 
 
    _listFilter: string; 
 
    constructor() { 
 
    this.filterMobiles = this.mobiles; 
 
    this.listFilter = ''; 
 
    } 
 
    get listFilter() { 
 
    return this._listFilter; 
 
    } 
 
    set listFilter(value) { 
 
    this._listFilter = value; 
 
    this.filterMobiles = this.listFilter ? this.performFilter(this.listFilter) : this.mobiles; 
 
    } 
 
    performFilter(filterBy: string): mobile[] { 
 
    filterBy = filterBy.toLocaleLowerCase(); 
 
    return this.mobiles.filter((mobile: mobile) => 
 
     mobile.mobile_name.toLocaleLowerCase().indexOf(filterBy) !== -1); 
 
    } 
 

 
}
/*rating.component.css*/ 
 
.crop { 
 
    overflow: hidden; 
 
} 
 

 
div { 
 
    cursor: pointer; 
 
} 
 
#star{ 
 
    color:gold; 
 
} 
 
/*mobile.componet.css/ 
 
h1,p{ 
 
    font-style: initial; 
 
    color:green; 
 
    text-align: center; 
 
    border: dashed; 
 
} 
 

 
td { 
 
    font-family: Tahoma; 
 
} 
 
.col-md-2{ 
 
    background-color: blue; 
 
    color: white; 
 
} 
 
h4 { 
 
    color: red; 
 
    text-align: right; 
 
    border-radius: 3px; 
 
    background-color: rgba(0, 0, 0, .1); 
 
    height: 20px; 
 
    padding: 3px 6px; 
 
    font-weight: 500; 
 
    display: inline-block; 
 
    line-height: 12px; 
 
    margin-left: 100px; 
 
} 
 
#clear{ 
 
    float:right; 
 
}
<!--rating.component.html--> 
 
<div class="crop" 
 
    [style.width.px]="starWidth" 
 
    [title]="rating"> 
 
    <div style="width: 86px" id="star"> 
 
    <span class="glyphicon glyphicon-star-empty"></span> 
 
    <span class="glyphicon glyphicon-star-empty"></span> 
 
    <span class="glyphicon glyphicon-star-empty"></span> 
 
    <span class="glyphicon glyphicon-star-empty"></span> 
 
    <span class="glyphicon glyphicon-star-empty"></span> 
 
    </div> 
 
</div> 
 

 
<!--mobiles.component.html--> 
 
<h1>MOBILE CART</h1> 
 
<div class="container"> 
 
    <div class='col-md-2'> 
 
    Refined by:{{listFilter}} 
 
    </div> 
 
    <div class='col-md-1'> 
 
    <input type="text" [(ngModel)]="listFilter"> 
 
    </div> 
 
</div> 
 

 
<!--<h4>{{cart}}</h4>--> 
 
<img src="{{shop}}" width="50" align="right" /> 
 

 
<table class="table "> 
 

 
    <tr> 
 
    <th> 
 
     <button class='btn btn-primary' (click)="displayImages()"> 
 
     Display Images 
 
     </button> 
 
    </th> 
 
    <!--<th>Image</th>--> 
 
    <th>NAME</th> 
 
    <th>PRICE</th> 
 
    <th>CODE</th> 
 
    <th>RELEASE DATE</th> 
 
    <th>RATING</th> 
 
    <th>CART </th> 
 
    </tr> 
 
    <tr *ngFor='let mobile of filterMobiles'> 
 
    <td> 
 
     <div [hidden]="showImages"> 
 
     <!--<img src="{{mobile.imageUrl}}" width="100" />--> 
 
     <img [src]="image+mobile.imageUrl" width="100" /> 
 
     </div> 
 
    </td> 
 

 
    <td> 
 
     {{mobile.mobile_name|uppercase}} 
 
    </td> 
 
    <td> 
 
     {{mobile.mobile_price|currency:'INR':'1.2-2' }} 
 
    </td> 
 
    <td> 
 
     {{mobile.mobile_code|separator:'_'}} 
 
    </td> 
 
    <td> 
 
     {{mobile.release_date|date:'longDate'}} 
 
    </td> 
 
    <td > 
 
     <!--{{mobile.rating}}--> 
 
     <app-rating [rating]='mobile.rating'></app-rating> 
 
    </td> 
 
    <td> 
 
     <button class="btn btn-primary" (click)="inCart()"> 
 
     Add to cart 
 
     </button> 
 
     <button class="btn btn-primary"> 
 
     Buy Now 
 
     </button> 
 
    </td> 
 
    </tr> 
 
</table> 
 
<p>{{cart}} items are added into the cart</p> 
 
<button class="btn btn-primary" (click)="clear()" id="clear"> 
 
    CLEAR 
 
</button>

감사합니다.

+0

.. .. 미리 감사를 좀 도와주세요 –

+0

당신은 당신의 코드를 재 배열하고 우리가 잘못 무슨 일이 일어나고 있는지 확인할 수 있도록이 하나의 패션에 넣을 수 있습니다 . 또는 jsfiddle를 만드십시오! –

+0

나는 두 개의 html 파일과 두 개의 타이프 스크립트 파일을 가지고있다. 하나의 html 파일을 다른 html 파일로 가져오고있다. –

답변

0

여기에서 문제는 앱이 렌더링되는 방식 인 것으로 보이므로 코드 샘플을 MCVE으로 낮추었습니다. 그래서 이것은 우리가 시작하는 곳이다 : 모든

.crop { 
 
    overflow: hidden; 
 
} 
 
#star { 
 
    color: gold; 
 
    width: 100px; 
 
} 
 
td { 
 
    font-family: Tahoma; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
<table class="table"> 
 
    <tr> 
 
    <td> 
 
     <div class="crop" title="rating"> 
 
     <div style="width: 86px" id="star"> 
 
      <span class="glyphicon glyphicon-star-empty"></span> 
 
      <span class="glyphicon glyphicon-star-empty"></span> 
 
      <span class="glyphicon glyphicon-star-empty"></span> 
 
      <span class="glyphicon glyphicon-star-empty"></span> 
 
      <span class="glyphicon glyphicon-star-empty"></span> 
 
     </div> 
 
     </div> 
 
    </td> 
 
    </tr> 
 
</table>

먼저, 별을 포함하는 당신의 포장 문제를 해결하기 위해 보인다 <td>에서 Tahoma을 제거. 내가 Tahoma의 글리프 너비가 별을 표시하는 데 사용되는 Bootstrap Glyphicons Halflings의 글리프 너비와 다르다고 가정합니다.

<td>에 클래스 이름을 적용하고 (예 : class="star-ratings") :not() 부정 의사 클래스를 사용하는 CSS에서이를 제외하면됩니다.

td:not(.star-ratings) { 
    font-family: Tahoma; 
} 

이것은 (적어도 Chrome에서는) 문제를 해결하는 것으로 보이지만 그 자체로는 매우 실용적인 해결책이 아닙니다. 상기 이외에 white-space:nowrap ~ #star을 적용하십시오. 이렇게하면 별이 두 번째 줄로 줄 바꿈되지 않습니다.

#star { 
    white-space: nowrap; 
} 

는 또한 #star에 적용 86px의 하드 코딩, 인라인 폭을 제거하는 것입니다. 인라인 스타일은 다른 규칙 세트보다 높은 구체성을 가지고 있으므로 CSS에 적용된 너비는 100px입니다. 이것은 다음과 같이 우리를 잎 :

.crop { 
 
    overflow: hidden; 
 
} 
 
#star { 
 
    color: gold; 
 
    width: 100px; 
 
    white-space: nowrap; 
 
} 
 
td:not(.star-ratings) { 
 
    font-family: Tahoma; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
 
<link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
<table class="table"> 
 
    <tr> 
 
    <td class="star-ratings"> 
 
     <div class="crop" title="rating"> 
 
     <div id="star"> 
 
      <span class="glyphicon glyphicon-star-empty"></span> 
 
      <span class="glyphicon glyphicon-star-empty"></span> 
 
      <span class="glyphicon glyphicon-star-empty"></span> 
 
      <span class="glyphicon glyphicon-star-empty"></span> 
 
      <span class="glyphicon glyphicon-star-empty"></span> 
 
     </div> 
 
     </div> 
 
    </td> 
 
    </tr> 
 
</table>

+0

Soooo Much. 글꼴 스타일 {Tahoma}을 제거한 후 코드가 실행되었습니다 ... –

+0

나는 그것이 효과적이라고 생각합니다! 그리고 당신이 'white-space : nowrap'을 가지고 있는지 확인하십시오. 오랫동안 더 실용적인 해결책이 될 것입니다. o) – agrm

+0

그래, 그것은'white-space : nowrap'와 함께 ... –

관련 문제