Angular7、嵌套函数使用同一个变量
Angular 7, nested function using same variable
我正在尝试 运行 一个函数 - "createThreeDates()" 可以使用相同的变量生成三个不同的结果(日期基于 -90、-60、-45)。用户在输入字段中设置日期后,将在函数中设置和使用该日期变量。 Generate 1 按钮调用该函数。如果我只需要一个约会(而不是三个),一切都很好。仅供参考,此函数使用 installed/import 包 "add-subtract-date."
我的代码中没有错误,但是函数 returns 浏览器中所有三个输入字段的相同日期 (fortyDaysDate) (?)。 -我不知道当我有不同的 ngModels 时这怎么可能。
这里是component.ts...
import { Component, EventEmitter, Input, Output, OnInit } from '@angular/core';
import { CalculatorService } from '../calculator.service';
import { add, subtract } from 'add-subtract-date';
import { NgForm, FormGroup } from '@angular/forms';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
public fortyDaysDate: any;
public sixtyDaysDate: any;
public ninetyDaysDate: any;
private _retireSet: Date;
@Input() set retireSet(date: Date) {
this._retireSet = new Date(date);
}
constructor(private calculatorService: CalculatorService) {
}
ngOnInit() {
}
public createThreeDates(): void {
let d: Date = this._retireSet;
let ninetyDaysBack = subtract(d, 90, "days");
this.ninetyDaysDate = ninetyDaysBack;
console.log('90 is ' + this.ninetyDaysDate);
let sixtyDaysBack = add(d, 30, "days");
this.sixtyDaysDate = sixtyDaysBack;
console.log('60 is ' + this.sixtyDaysDate);
let fortyDaysBack = add(d, 15, "days");
this.fortyDaysDate = fortyDaysBack;
console.log('45 is ' + this.fortyDaysDate);
// the value of sixtyDaysDate changes to fortyDaysDate value here...
console.log('60 is ' + this.sixtyDaysDate);
}
}
}
这里是component.html...
<div class="container">
<form class="ret-form" #calcForm="ngForm" novalidate (ngSubmit)="onSubmit(calcForm)">
<div class="form-group">
<label for="retireSet">Set Date</label>
<input type="datetime-local" id="retireSet" name="RetireCalculatorSetDate" value="retireSet"
ngModel #RetireCalculatorSetDate="ngModel" [(ngModel)]="retireSet" class="form-control" />
</div>
<div>
<button type="button" class="btn btn-lg btn-outline-dark" (click)="createThreeDates()">Generate 1</button>
<div>
<input type="text" name="RetireCalculatorDay90" value="ninetyDaysDate" ngModel #RetireCalculatorDay90="ngModel"
[(ngModel)]="ninetyDaysDate" class="form-control" />
</div>
<div>
<input type="text" name="RetireCalculatorDay60" value="sixtyDaysDate" ngModel #RetireCalculatorDay60="ngModel"
[(ngModel)]="sixtyDaysDate" class="form-control" />
</div>
<input type="text" name="RetireCalculatorDay45" value="fortyDaysDate" ngModel #RetireCalculatorDay45="ngModel"
[(ngModel)]="fortyDaysDate" class="form-control" />
</div>
</form>
</div>
与其说这是一个答案,不如说这是一次代码审查。
您的语法不正确。
- 去掉函数内的花括号。
- 确保函数 "subtract" 没有改变传递的变量
在。你可能想要 "clone" 变量“_retireSet”,这样你
不在这个函数中改变它。
- 如果您不重新分配变量,请使用 "const"。在这个函数中
你是所以你需要使用 "let".
- 如@crashmstr 所述,将函数 return 类型更改为 void
旁注:
函数名称令人困惑。 "getAllDates" 这意味着您正在 return 处理一个带有日期的对象。
而是将名称更改为 prepareAllDates 或 createAllDates 或类似名称。
请记住,"Set" 和 "Get" 分别表示 "Setting a Variable" 和 "Getting a Variable"。
** 更新 **
从您下面的评论来看,您似乎遇到了 javascript 本身的问题。
按值分配
var batman = 7;
var superman = batman; //assign-by-value
superman++;
console.log(batman); //7
console.log(superman); //8
按引用赋值
var flash = [8,8,8];
var quicksilver = flash; //assign-by-reference
quicksilver.push(0);
console.log(flash); //[8,8,8,0]
console.log(quicksilver); //[8,8,8,0]
针对您的具体问题:
var x = new Date()
var y = new Date(x)
x.setFullYear('2019');
x //Date 2019-11-01T13:59:56.083Z
y //Date 2018-11-01T13:59:56.083Z
根据我的建议 post,这里是解决问题的有效代码(仅供参考)。
component.HTML...
<div class="form-group">
<label for="retireSet">Set Retirement Date</label>
<input type="datetime-local" id="retireSet" name="RetireCalculatorSetDate" ngModel #RetireCalculatorSetDate="ngModel"
[(ngModel)]="retireSet" [(ngModel)]="retireSet60" [(ngModel)]="retireSet45"
class="form-control" /> <div>
<div><button type="button" class="btn btn-light" (click)="createThreeDates()">Calculate Dates</button> </div>
<div>
<input type="hidden" name="RetireCalculatorDay90" value="ninetyDaysDate" ngModel #RetireCalculatorDay90="ngModel"
[(ngModel)]="ninetyDaysDate" class="form-control" />
<p class="textNinety"> 90 day notice is due {{ ninetyDaysDate | date: "longDate" }}. </p>
</div>
<div>
<input type="hidden" name="RetireCalculatorDay60" value="sixtyDaysDate" ngModel #RetireCalculatorDay60="ngModel"
[(ngModel)]="sixtyDaysDate" class="form-control" />
<p> 60 day notice is due {{ sixtyDaysDate | date: "longDate" }}. </p>
</div>
<div>
<input type="hidden" name="RetireCalculatorDay45" value="fortyDaysDate" ngModel #RetireCalculatorDay45="ngModel"
[(ngModel)]="fortyDaysDate" class="form-control" />
<p> 45 day notice is due {{ fortyDaysDate | date: "longDate" }}. </p>
</div>
component.ts...
import { Component, EventEmitter, Input, Output, OnInit } from '@angular/core';
import { CalculatorService } from '../calculator.service';
import { add, subtract } from 'add-subtract-date';
import { NgForm, FormGroup } from '@angular/forms';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
public fortyDaysDate: any;
public sixtyDaysDate: any;
public ninetyDaysDate: any;
public myRetDatePlus45: Date;
public myRetDatePlus60: Date;
public myRetDatePlus90: Date;
private _retireSet: Date;
@Input() set retireSet(date: Date) {
this._retireSet = new Date(date);
this.myRetDatePlus90 = this._retireSet;
}
private _retireSet60: Date;
@Input() set retireSet60(date: Date) {
this._retireSet60 = new Date(date);
this.myRetDatePlus60 = this._retireSet60;
}
private _retireSet45: Date;
@Input() set retireSet45(date: Date) {
this._retireSet45 = new Date(date);
this.myRetDatePlus45 = this._retireSet45;
}
constructor(private calculatorService: CalculatorService) {
}
ngOnInit() {
}
public createThreeDates(): void {
let dNinety: Date = this.myRetDatePlus90;
let ninetyDaysBack = subtract(dNinety, 90, "days");
this.ninetyDaysDate = ninetyDaysBack;
let dSixty: Date = this.myRetDatePlus60;
let sixtyDaysBack = subtract(dSixty, 60, "days");
this.sixtyDaysDate = sixtyDaysBack;
console.log('60 is ' + this.sixtyDaysDate);
let dForty: Date = this.myRetDatePlus45;
let fortyDaysBack = subtract(dForty, 45, "days");
this.fortyDaysDate = fortyDaysBack;
}
我正在尝试 运行 一个函数 - "createThreeDates()" 可以使用相同的变量生成三个不同的结果(日期基于 -90、-60、-45)。用户在输入字段中设置日期后,将在函数中设置和使用该日期变量。 Generate 1 按钮调用该函数。如果我只需要一个约会(而不是三个),一切都很好。仅供参考,此函数使用 installed/import 包 "add-subtract-date."
我的代码中没有错误,但是函数 returns 浏览器中所有三个输入字段的相同日期 (fortyDaysDate) (?)。 -我不知道当我有不同的 ngModels 时这怎么可能。
这里是component.ts...
import { Component, EventEmitter, Input, Output, OnInit } from '@angular/core';
import { CalculatorService } from '../calculator.service';
import { add, subtract } from 'add-subtract-date';
import { NgForm, FormGroup } from '@angular/forms';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
public fortyDaysDate: any;
public sixtyDaysDate: any;
public ninetyDaysDate: any;
private _retireSet: Date;
@Input() set retireSet(date: Date) {
this._retireSet = new Date(date);
}
constructor(private calculatorService: CalculatorService) {
}
ngOnInit() {
}
public createThreeDates(): void {
let d: Date = this._retireSet;
let ninetyDaysBack = subtract(d, 90, "days");
this.ninetyDaysDate = ninetyDaysBack;
console.log('90 is ' + this.ninetyDaysDate);
let sixtyDaysBack = add(d, 30, "days");
this.sixtyDaysDate = sixtyDaysBack;
console.log('60 is ' + this.sixtyDaysDate);
let fortyDaysBack = add(d, 15, "days");
this.fortyDaysDate = fortyDaysBack;
console.log('45 is ' + this.fortyDaysDate);
// the value of sixtyDaysDate changes to fortyDaysDate value here...
console.log('60 is ' + this.sixtyDaysDate);
}
}
}
这里是component.html...
<div class="container">
<form class="ret-form" #calcForm="ngForm" novalidate (ngSubmit)="onSubmit(calcForm)">
<div class="form-group">
<label for="retireSet">Set Date</label>
<input type="datetime-local" id="retireSet" name="RetireCalculatorSetDate" value="retireSet"
ngModel #RetireCalculatorSetDate="ngModel" [(ngModel)]="retireSet" class="form-control" />
</div>
<div>
<button type="button" class="btn btn-lg btn-outline-dark" (click)="createThreeDates()">Generate 1</button>
<div>
<input type="text" name="RetireCalculatorDay90" value="ninetyDaysDate" ngModel #RetireCalculatorDay90="ngModel"
[(ngModel)]="ninetyDaysDate" class="form-control" />
</div>
<div>
<input type="text" name="RetireCalculatorDay60" value="sixtyDaysDate" ngModel #RetireCalculatorDay60="ngModel"
[(ngModel)]="sixtyDaysDate" class="form-control" />
</div>
<input type="text" name="RetireCalculatorDay45" value="fortyDaysDate" ngModel #RetireCalculatorDay45="ngModel"
[(ngModel)]="fortyDaysDate" class="form-control" />
</div>
</form>
</div>
与其说这是一个答案,不如说这是一次代码审查。 您的语法不正确。
- 去掉函数内的花括号。
- 确保函数 "subtract" 没有改变传递的变量 在。你可能想要 "clone" 变量“_retireSet”,这样你 不在这个函数中改变它。
- 如果您不重新分配变量,请使用 "const"。在这个函数中 你是所以你需要使用 "let".
- 如@crashmstr 所述,将函数 return 类型更改为 void
旁注: 函数名称令人困惑。 "getAllDates" 这意味着您正在 return 处理一个带有日期的对象。 而是将名称更改为 prepareAllDates 或 createAllDates 或类似名称。
请记住,"Set" 和 "Get" 分别表示 "Setting a Variable" 和 "Getting a Variable"。
** 更新 ** 从您下面的评论来看,您似乎遇到了 javascript 本身的问题。
按值分配
var batman = 7;
var superman = batman; //assign-by-value
superman++;
console.log(batman); //7
console.log(superman); //8
按引用赋值
var flash = [8,8,8];
var quicksilver = flash; //assign-by-reference
quicksilver.push(0);
console.log(flash); //[8,8,8,0]
console.log(quicksilver); //[8,8,8,0]
针对您的具体问题:
var x = new Date()
var y = new Date(x)
x.setFullYear('2019');
x //Date 2019-11-01T13:59:56.083Z
y //Date 2018-11-01T13:59:56.083Z
根据我的建议 post,这里是解决问题的有效代码(仅供参考)。
component.HTML...
<div class="form-group">
<label for="retireSet">Set Retirement Date</label>
<input type="datetime-local" id="retireSet" name="RetireCalculatorSetDate" ngModel #RetireCalculatorSetDate="ngModel"
[(ngModel)]="retireSet" [(ngModel)]="retireSet60" [(ngModel)]="retireSet45"
class="form-control" /> <div>
<div><button type="button" class="btn btn-light" (click)="createThreeDates()">Calculate Dates</button> </div>
<div>
<input type="hidden" name="RetireCalculatorDay90" value="ninetyDaysDate" ngModel #RetireCalculatorDay90="ngModel"
[(ngModel)]="ninetyDaysDate" class="form-control" />
<p class="textNinety"> 90 day notice is due {{ ninetyDaysDate | date: "longDate" }}. </p>
</div>
<div>
<input type="hidden" name="RetireCalculatorDay60" value="sixtyDaysDate" ngModel #RetireCalculatorDay60="ngModel"
[(ngModel)]="sixtyDaysDate" class="form-control" />
<p> 60 day notice is due {{ sixtyDaysDate | date: "longDate" }}. </p>
</div>
<div>
<input type="hidden" name="RetireCalculatorDay45" value="fortyDaysDate" ngModel #RetireCalculatorDay45="ngModel"
[(ngModel)]="fortyDaysDate" class="form-control" />
<p> 45 day notice is due {{ fortyDaysDate | date: "longDate" }}. </p>
</div>
component.ts...
import { Component, EventEmitter, Input, Output, OnInit } from '@angular/core';
import { CalculatorService } from '../calculator.service';
import { add, subtract } from 'add-subtract-date';
import { NgForm, FormGroup } from '@angular/forms';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
public fortyDaysDate: any;
public sixtyDaysDate: any;
public ninetyDaysDate: any;
public myRetDatePlus45: Date;
public myRetDatePlus60: Date;
public myRetDatePlus90: Date;
private _retireSet: Date;
@Input() set retireSet(date: Date) {
this._retireSet = new Date(date);
this.myRetDatePlus90 = this._retireSet;
}
private _retireSet60: Date;
@Input() set retireSet60(date: Date) {
this._retireSet60 = new Date(date);
this.myRetDatePlus60 = this._retireSet60;
}
private _retireSet45: Date;
@Input() set retireSet45(date: Date) {
this._retireSet45 = new Date(date);
this.myRetDatePlus45 = this._retireSet45;
}
constructor(private calculatorService: CalculatorService) {
}
ngOnInit() {
}
public createThreeDates(): void {
let dNinety: Date = this.myRetDatePlus90;
let ninetyDaysBack = subtract(dNinety, 90, "days");
this.ninetyDaysDate = ninetyDaysBack;
let dSixty: Date = this.myRetDatePlus60;
let sixtyDaysBack = subtract(dSixty, 60, "days");
this.sixtyDaysDate = sixtyDaysBack;
console.log('60 is ' + this.sixtyDaysDate);
let dForty: Date = this.myRetDatePlus45;
let fortyDaysBack = subtract(dForty, 45, "days");
this.fortyDaysDate = fortyDaysBack;
}