我目前正在将我的 ASP.NET MVC 5 Web 应用程序的部分内容更新为 AngularJS 1.5。
由于并非所有内容都是 Angular js,因此某些部分是使用 jQuery 完成的。
在 angularjs 之前我有
<h2>@Model.title</h2>
变成<h2>Document XYZ</h2>
转移到 Angular 后我有一个
<h2>{{ document.title }}</h2>
我试图从中获取文档标题并通过 jQuery 选择器在其他地方使用它
$('#documentId').find("h2").text()
不幸的是,由于更改为 Angular ,我得到的只是表达式 {{ document.title }}
,但不是成功渲染的内容。
有什么想法吗?
最佳答案
在将 HTML 字符串插入 DOM 之前,您需要对 HTML 字符串调用 $compile,以便 Angular 有机会执行绑定(bind)。
在你的 fiddle 中,它看起来像这样。
$("#dynamicContent").html(
$compile(
"<h2>{{ document.title }}</h2>"
)(scope)
);
如需更多引用,请查看此链接 click here
关于javascript - 使用 jquery 从 Angular 表达式中获取内容失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36568971/