Javascript 动态创建要在modal中使用的类
我有以下用于显示模式的代码: app.htmlJavascript 动态创建要在modal中使用的类,javascript,jquery,html,aurelia,Javascript,Jquery,Html,Aurelia,我有以下用于显示模式的代码: app.html <a click.delegate="setModal('person-information', 'Tom', 'Hanks')">Test</a> person-information.html <template> <form role="form"> <div class="form-group"> <label
<a click.delegate="setModal('person-information', 'Tom', 'Hanks')">Test</a>
person-information.html
<template>
<form role="form">
<div class="form-group">
<label for="fn">First Name</label>
<input type="text" value.bind="person.firstName" class="form-control" id="fn" placeholder="first name">
</div>
<div class="form-group">
<label for="ln">Last Name</label>
<input type="text" value.bind="person.lastName" class="form-control" id="ln" placeholder="last name">
</div>
</form>
</template>
此代码适用于显示以下内容:
我很难弄清楚如何注入自己的数据来动态创建一个“人”
伪代码:
app.html
<a click.delegate="setModal('person-information', 'Tom', 'Hanks')">Test</a>
有人有过这样做的经验吗?将您的属性(firstName和lastName)更改为可写。可写:对。大概是这样的:
firstName: {
value: "Tom",
writable: true
}
lastName: {
value: "Hanks",
writable: true
}
更多信息请访问:以防有所帮助。首先,错误是因为contentModal属性是简单的字符串“person information”,因此它没有定义任何person属性 但您的代码中也存在概念问题: 您正在使用content.bind将内容动态分配给模态元素,因此您的app.js类不知道内容,您不应该尝试在app.js类中分配内容属性值 为了保持通用性,您可以执行以下操作(未经测试) 并称之为
<a click.delegate="setModal('person-information', { firstName: 'Tom', secondName: 'Hanks' })">Test</a>
更新:
此解决方案当前不起作用:
setModal
是否没有得到正确的值或什么?@PWKad调用setModal函数时,我在js控制台中遇到以下错误:Uncaught TypeError:无法设置未定义的属性“firstName”
如果我尝试这样做:this.contentModal.person=new person();
I get:Uncaught TypeError:无法分配给引发未定义错误的人员信息的只读属性“person”
:不幸的是:Uncaught TypeError:无法设置未定义的属性“firstName”
我将尝试调试setModal方法来检查此的内容。contentModal和this.contentModal.person.yep this.contentModal只是一个字符串,它引用了人员信息
查看模式。更新后的setModal
方法只是伪代码,它只是为了说明我想要发生什么。我尝试了一下更新后的答案,如何才能调用created(…)
,如果它自动发生呢?我已经测试过,同样的情况也发生在我身上,created没有被调用。有一个问题。
setModal(modal, firstname, lastname) {
this.contentModal = modal;
this.contentModal.Person.firstName = firstname;
this.contentModal.Person.lastName = lastname;
$('.modal').modal();
}
firstName: {
value: "Tom",
writable: true
}
lastName: {
value: "Hanks",
writable: true
}
setModal(modal, props) {
this.contentModal = modal;
this.modalProps = props;
...
}
<a click.delegate="setModal('person-information', { firstName: 'Tom', secondName: 'Hanks' })">Test</a>
bind(bindingContext) {
this.person.firstName = bindingContext.modalProps['firstName'];
....
}