如何在angular中设置空值
这与我关于角度空值的帖子有关 我试图理解为什么我的应用程序/代码在当前情况下会表现出不同的行为如何在angular中设置空值,angular,Angular,这与我关于角度空值的帖子有关 我试图理解为什么我的应用程序/代码在当前情况下会表现出不同的行为 calculateAge(dateOne: Date, dateTwo: Date): number { let firstDate = new Date(dateOne); let secondDate = new Date(dateTwo); let differenceInDates = Math.abs(firstDate.getTime() - secondDat
calculateAge(dateOne: Date, dateTwo: Date): number {
let firstDate = new Date(dateOne);
let secondDate = new Date(dateTwo);
let differenceInDates = Math.abs(firstDate.getTime() - secondDate.getTime());
let age = Math.floor(differenceInDates / (1000 * 3600 * 24));
console.log(age);
return age;
}
getFlowers() {
this.flowerSubscription = this.flowerService.getFlowersByFlowerId(this.flowerId, this.someName).subscribe(
res => {
this.dataToSend = res.DataToSend
this.flowerSubscription = this.flowerService.getFlowers(this.dataToSend, this.someName).subscribe(
res => {
this.flower = res; // this.Flower is interface IFlower
console.log(this.flower); <--- this is null
if (this.flower !== undefined) {
this.flowerNumber = (this.flower.FNumber || '--').toString();
this.flowerInDate = this.flower.FlowerInDate;
this.flowerOutDate = this.flower.FlowerInDate;
this.flowerAge = this.calculateAge(this.flower.flowerWarehouseDate, this.flower.flowerStoreDate);
this.flowerStatus = (this.flower.FlowerStatus || '--');
this.customerAmount = (this.flower.CustomerAmount || '--').toString();
this.warehouseAmount = (this.flower.WarehouseAmount || '--').toString();
}
}
}
}
calculateAge(日期一:日期,日期二:日期):数字{
设firstDate=新日期(dateOne);
设第二个日期=新日期(日期二);
让differenceInDates=Math.abs(firstDate.getTime()-secondDate.getTime());
让年龄=数学楼层(差异数/(1000*3600*24));
控制台日志(年龄);
回归年龄;
}
getFlowers(){
this.flowerSubscription=this.flowerService.getFlowersByFlowerId(this.flowerId,this.someName)。订阅(
res=>{
this.dataToSend=res.dataToSend
this.flowerSubscription=this.flowerService.getFlowers(this.dataToSend,this.someName)。订阅(
res=>{
this.flower=res;//this.flower是花椰菜
console.log(这个花);Maddy
getFlowers(){
this.flowerSubscription=this.flowerService.getFlowersByFlowerId(this.flowerId,this.someName)。订阅(
res=>{
this.dataToSend=res.dataToSend
this.flowerSubscription=this.flowerService.getFlowers(this.dataToSend,this.someName)。订阅(
res=>{
this.flower=res;//this.flower是花椰菜
console.log(this.flower);我通过检查this.dataToSend
是否为null找到了解决方案。如果为null,我将所有值设置为--
,否则我将使用实际变量设置值。
@Eliseo,谢谢你的帮助。if(variable)相当于if(variable!=未定义和变量!=null和变量!=0和变量!=”),if(!variable)相当于if(variable==未定义和变量==null | | variable==0 | | | variable=“”)@Eliseo,不应该|
处理空值吗?你的意思是console.log(this.flower;如果this.flower
为null,那么this.flower.FNumber
将抛出一个错误。你有错误吗?@user184994,我只得到null异常。否则没有错误。Eliseo,也许我不完全理解,如果你能帮我解释一下。在我的If语句中,“| |”应该验证null。对吗?还有,如果只有null异常呢有一件事在if条件下是空的。我认为如果是这样的话,这个会失败。@Maddy,this.flower可以是空的,也可以不是空的。如果this.flower不为空,this.flower.FlowerStatus可以是空的。我认为this.FlowerStatus=this.FlowerStatus | | |-'一定是好的。另一个说Hyuc Kang this.FlowerStatus=this.FlowerStatus=this.FlowerStatus?th的语句是空的is.flower.FlowerStatus:“--”也可以(是三元运算符)。但最后两条语句给出的错误是this.flower为null
<table id="FlowerDetails" class="f-fs">
<tr>
<td class="f-title">Flower Nmber</td>
<td class="f-text">{{flowerNumber}}</td>
</tr>
<tr>
<td class="f-title">flowerInDate</td>
<td class="f-text">{{(flowerInDate != null) ? (claimStartDate | date: 'shortDate') : '--'}}</td>
</tr>
<tr>
<td class="f-title">flowerOutDate</td>
<td class="f-text">{{(flowerOutDate != null) ? (flowerOutDate | date: 'shortDate') : '--'}}</td>
</tr>
<tr>
<td class="f-title">Age</td>
<td class="f-text">{{flowerAge}}</td>
</tr>
<tr>
<td class="f-title">Flower Status</td>
<td class="f-text">{{flowerStatus}}</td>
</tr>
</table>
getFlowers() {
this.flowerSubscription = this.flowerService.getFlowersByFlowerId(this.flowerId, this.someName).subscribe(
res => {
this.dataToSend = res.DataToSend
this.flowerSubscription = this.flowerService.getFlowers(this.dataToSend, this.someName).subscribe(
res => {
this.flower = res; // this.Flower is interface IFlower
console.log(this.flower); <--- this is null
if (this.flower){ //if this.flower <> undefined and <>null
this.flowerNumber = (this.flower.FNumber || '--').toString();
this.flowerInDate = this.flower.FlowerInDate;
this.flowerOutDate = this.flower.FlowerInDate;
this.flowerAge = this.calculateAge(this.flower.flowerWarehouseDate, this.flower.flowerStoreDate);
this.flowerStatus = (this.flower.FlowerStatus || '--');
this.customerAmount = (this.flower.CustomerAmount || '--').toString();
this.warehouseAmount = (this.flower.WarehouseAmount || '--').toString();
}
else
{
this.flowerNumber = '--';
this.flowerInDate = '--';
this.flowerOutDate = '--';
this.flowerAge = 0;
this.flowerStatus = '--';
this.customerAmount = '--';
this.warehouseAmount = '--';
}
}
}