[英]Data-bind return html a href with data-bind
i have a problem with data-bind into a return observable,我在将数据绑定到返回可观察对象时遇到问题,
My html code:我的 html 代码:
<form data-bind="submit: capitalizeNames">
<p><label>Prénom : <input data-bind="value: firstName" /></label></p>
<p><label>Nom: <input data-bind="value: lastName" /></label></p>
<p>Nom complet: <strong data-bind="html: fullName" ></strong></p>
<button type="submit">Valider</button>
</form>
My js knwockout:我的 js 淘汰赛:
self.fullName = ko.computed(function() {
var firstName = self.firstName();
return '<a data-bind="click: $root.test" href="#">' + firstName + '</a>';
});
self.test = function() {
console.log("test");
}
I can't trigger test function我无法触发测试 function
What is solution plz?请问什么是解决方案? Thank you !
谢谢 !
The html
binding does not applyBindings
to the content it injects. html
绑定不应用applyBindings
到它注入的内容。 In this particular example I think you're best of using the text
binding.在这个特定的示例中,我认为您最好使用
text
绑定。 You can put the <a>
markup in your HTML instead of your viewmodel.您可以将
<a>
标记放在 HTML 而不是视图模型中。
function ViewModel() { var self = this; self.firstName = ko.observable("Jane"); self.lastName = ko.observable("Doe"); self.fullName = ko.computed(function() { var firstName = self.firstName(); return firstName; }); self.test = function() { console.log("test"); } } ko.applyBindings(new ViewModel());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script> <p><label>Prénom: <input data-bind="value: firstName" /></label></p> <p><label>Nom: <input data-bind="value: lastName" /></label></p> <p>Nom complet: <strong><a data-bind="text: fullName, click: test" href="#"></a></strong></p>
If you want to separate your <a>
markup from the main HTML, it's best to use the template
binding or a knockout component .如果您想将
<a>
标记与主 HTML 分开,最好使用template
绑定或敲除组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.