Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 在角度6中,从其他div值设置div值_Angular - Fatal编程技术网

Angular 在角度6中,从其他div值设置div值

Angular 在角度6中,从其他div值设置div值,angular,Angular,我想根据另一个div值设置一个div值: <div #priceRef>{{ price(9) }}</div> <div>{{ priceRef }}</div> {{price(9)} {{priceRef}} 更复杂的示例是,我想计算2个div值的差值: <div #total1Ref>{{ total(1) }}</div> <div #total2Ref>{{ total(2) }}</div

我想根据另一个div值设置一个div值:

<div #priceRef>{{ price(9) }}</div>
<div>{{ priceRef }}</div>
{{price(9)}
{{priceRef}}
更复杂的示例是,我想计算2个div值的差值:

<div #total1Ref>{{ total(1) }}</div>
<div #total2Ref>{{ total(2) }}</div>
<div>{{ total1Ref - total2Ref }}</div>
{{total(1)}
{{总计(2)}
{{total1Ref-total2Ref}}

这里的想法是避免额外调用
price
total
,以节省时间

您可以通过
innerText
属性访问文本,仅当文本中只有数字时,此选项才有效

<div #total1Ref>{{ total(1) }}</div>
<div #total2Ref>{{ total(2) }}</div>
<div>{{ total1Ref.innerText - total2Ref.innerText }}</div>
{{total(1)}
{{总计(2)}
{{total1Ref.innerText-total2Ref.innerText}

您可以通过
innerText
属性访问文本,仅当文本中只有数字时,此选项才有效

<div #total1Ref>{{ total(1) }}</div>
<div #total2Ref>{{ total(2) }}</div>
<div>{{ total1Ref.innerText - total2Ref.innerText }}</div>
{{total(1)}
{{总计(2)}
{{total1Ref.innerText-total2Ref.innerText}

例如,如果使用带有value属性的选项,则可以利用value属性完成所需的任务

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `<option #total1Ref value={{value1}}>{{value1}}</option>
            <option #total2Ref value={{value2}}>{{value2}}</option>
            <div>{{ total1Ref.value - total2Ref.value }}</div>`,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  value1 = 10;
  value2 = 5;   
}
从'@angular/core'导入{Component};
@组成部分({
选择器:“我的应用程序”,
模板:`{value1}}
{{value2}}
{{total1Ref.value-total2Ref.value}}`,
样式URL:['./app.component.css']
})
导出类AppComponent{
值1=10;
值2=5;
}
修订版:

下面是使用函数的示例

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `<input #total1Ref value={{total(20)}} style="border: 0;"/>
            <input #total2Ref value={{total(10)}} style="border: 0;"/>
            <div>{{ total1Ref.value - total2Ref.value }}</div>`,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  value1 = 10;
  value2 = 5;  

  total(num){
    return this.value1 + this.value2 + num
  } 
}
从'@angular/core'导入{Component};
@组成部分({
选择器:“我的应用程序”,
模板:`
{{total1Ref.value-total2Ref.value}}`,
样式URL:['./app.component.css']
})
导出类AppComponent{
值1=10;
值2=5;
总数(个){
返回this.value1+this.value2+num
} 
}

例如,如果使用带有value属性的选项,则可以利用value属性完成所需的任务

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `<option #total1Ref value={{value1}}>{{value1}}</option>
            <option #total2Ref value={{value2}}>{{value2}}</option>
            <div>{{ total1Ref.value - total2Ref.value }}</div>`,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  value1 = 10;
  value2 = 5;   
}
从'@angular/core'导入{Component};
@组成部分({
选择器:“我的应用程序”,
模板:`{value1}}
{{value2}}
{{total1Ref.value-total2Ref.value}}`,
样式URL:['./app.component.css']
})
导出类AppComponent{
值1=10;
值2=5;
}
修订版:

下面是使用函数的示例

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  template: `<input #total1Ref value={{total(20)}} style="border: 0;"/>
            <input #total2Ref value={{total(10)}} style="border: 0;"/>
            <div>{{ total1Ref.value - total2Ref.value }}</div>`,
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  value1 = 10;
  value2 = 5;  

  total(num){
    return this.value1 + this.value2 + num
  } 
}
从'@angular/core'导入{Component};
@组成部分({
选择器:“我的应用程序”,
模板:`
{{total1Ref.value-total2Ref.value}}`,
样式URL:['./app.component.css']
})
导出类AppComponent{
值1=10;
值2=5;
总数(个){
返回this.value1+this.value2+num
} 
}


在组件中一次性计算这些总数,并将结果保存在字段中。这样,您就不需要一次又一次地编译。请注意,差分计算只是导致重新计算总数的原因之一。除非您的组件使用OnPush策略,否则每次更改检测都会导致重新计算总计。不相关,但出于性能原因,您应该避免将函数绑定到模板。如果这些函数是纯函数,更干净/更好的方法是使用pipes@JBNizet我无法一次性计算总数,因为总数是元素的总和,UI允许添加/删除元素。因此,对于每次修改,我需要重新计算总数。但我想每次修改一次,然后重复使用这个总数。这正是你应该做的。在需要重新计算时重新计算它们。就这么做吧。在你的组件中一次性计算这些总数,并将结果保存在字段中。这样,您就不需要一次又一次地编译。请注意,差分计算只是导致重新计算总数的原因之一。除非您的组件使用OnPush策略,否则每次更改检测都会导致重新计算总计。不相关,但出于性能原因,您应该避免将函数绑定到模板。如果这些函数是纯函数,更干净/更好的方法是使用pipes@JBNizet我无法一次性计算总数,因为总数是元素的总和,UI允许添加/删除元素。因此,对于每次修改,我需要重新计算总数。但我想每次修改一次,然后重复使用这个总数。这正是你应该做的。在需要重新计算时重新计算它们。就这么做。如何使用函数并调用一次?请参阅更新版本,使用无边界输入将是只调用一次函数的一个选项。感谢修订版本,它似乎与输入一起工作,但我使用div,它与div不工作,这就是为什么我把它放在问题标题中。明白吗,如果使用除div以外的其他工具不是选项,则您需要探索其他答案。如何使用函数并调用一次?请参阅更新版本,使用无边界输入将是仅调用一次函数的一个选项。感谢修订版本,它似乎与输入一起工作,但我使用div,它与div不起作用,这就是为什么我把它放在我的问题标题中。明白,如果使用div-us以外的其他工具不是一个选项,你将需要探索其他答案。谢谢,正是我想要的。谢谢,正是我想要的。