如何获得Angular 13中的自定义属性值
我有这样的下拉列表,我需要在按钮上获得值。但是它总是返回零。需要获取CUS_ID值,而无需jQuery
HTML文件
<select #myselect>
<option *ngFor="let title of titleArray" [attr.data-id]="title.cus_id" [value]="title.Value">
{{title.Text}}
</option>
</select>
<button (click)="save(myselect)">Save data</button>
TS文件
save(e:any){
let cus_id=e.getAttribute('data-id');
alert(cus_id)
}
I have a dropdown like this and I need to get the value on button click. but it always returns null. Need to get the cus_id value, without jquery
html file
<select #myselect>
<option *ngFor="let title of titleArray" [attr.data-id]="title.cus_id" [value]="title.Value">
{{title.Text}}
</option>
</select>
<button (click)="save(myselect)">Save data</button>
ts file
save(e:any){
let cus_id=e.getAttribute('data-id');
alert(cus_id)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我们可以创建两种绑定,并将“选定”值选项保存到变量。因为这是常规的HTML选择,我们使用
[(ngmodel)]
进行两种绑定。另外,使用JavaScript
title.text.text
- &gt;title.text
,mySelect
- &gt;mySelect
,title.value
- &gt;title.Value
等。然后,我们可以访问变量
选择
在上处理
(单击)事件,触发功能onsave()
/代码>单击代码的打字稿部分。我使用
?
访问属性以有条件地检查道具时首先存在,以防万一。我使用
parseint()
将cus_id
从模板作为字符串,回到一个数字。如果您不打算覆盖变量优先
const
而不是让
。避免
任何
类型e:任何
- &gt;e:事件
如果可能的话(尽管在我的示例中不需要)。如果可能的话,始终更喜欢使用
接口
或类
的强键打字,从长远来看,它将为您提供帮助,这就是打字稿的美 - 我们强大的东西。html
这可以进一步简化而不是使用
[value] =“ title?.cus_id”
我们可以做[value] =“ title”
并传递OBJtitle
to variable选择
,那么我们就不必从titlearray []
或使用parseint()
interpallate()代码> - 但这取决于您。工作示例: https://stackblitz.com/edit/Edit/EDIT/Angular5-select-option-option-dropdown-qt4yvp?file=src%2fapp%2fapp%2fapp%2fapp%2fapp; 。
We could create a two way binding and save the "selected" value option to a variable. Because this is a regular html select we use
[(ngModel)]
for two way binding.Also, we also normally use camelCase for property names when using javascript
title.Text
->title.text
,myselect
->mySelect
,title.Value
->title.value
etc.We can then access the variable
selected
for processing on(click)
event which triggers functiononSave()
click in typescript part of the code.I use
?
when accessing property to conditionally check the prop exists first, this is just in case.I use
parseInt()
to turncus_id
coming from template as string, back to a number.If you don't plan on overwriting the variable prefer
const
overlet
.Avoid
any
typee:any
->e: Event
if possible (though not needed in my example).If possible always prefer strong typing with
interface
orclass
, it will help you in the long run, that's the beauty of typescript - we strong type stuff.Html
This could be further simplified instead of using
[value]="title?.cus_id"
we could do[value]="title"
and pass the objtitle
to variableselected
, then we wouldn't have to search it fromtitleArray[]
nor interpolate strings to number usingparseInt()
- but that's up to you.Working example: https://stackblitz.com/edit/angular5-select-option-dropdown-qt4yvp?file=src%2Fapp%2Fapp.component.html,src%2Fapp%2Fapp.component.ts,src%2Findex.html
Welcome to SO, nicely formatted question.
在Angular中,我们有两种正确的方式来管理形式,构建反应性形式和模板驱动的形式。我建议您在
也就是说,我认为要使用模板驱动的表单最适合您的特定用例,并且您最终希望获得所选
title> title
的cus_id
属性。步骤1:导入
formsModule
在您的应用/root模块中(或声明组件的模块),导入
formsmodule
。In Angular, we have two proper ways of managing forms, building reactive forms, and template-driven forms. I recommend you to check them out in Introduction to forms in Angular.
That said, I presume going for template-driven form is best for your particular use-case, and you ultimately want to get the
cus_id
property of the selectedtitle
.Step 1: Import
FormsModule
In your app/root module (or module where your component is declared), import
FormsModule
.Step 2: Declare a data model for holding the current selection
Step 3: Bind data model to view
Connect the data model to the view such that whenever the user selects a different option, the value of the data model will automatically update.
Step 4: Process the selected option
Once the user hits save, you will now be able to process the actual object corresponding to the option that the user has currently selected.