javascript 如何加载 MVC 部分视图并保持 Angular JS 工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30123417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 11:44:40  来源:igfitidea点击:

How to load MVC Partial View and keep Angular JS working

javascriptasp.netasp.net-mvcangularjsasp.net-mvc-4

提问by arnoldrob

How should I load mvc partial views and keep Angular JS working. Angular JS is working fine after I render the layout, but if I load partial views in the layout, Angular JS is not working anymore in the partials. I guess that Angular engine is not compiling the ng- directives after I inject the HTML in the DOM.

我应该如何加载 mvc 部分视图并保持 Angular JS 工作。渲染布局后,Angular JS 工作正常,但如果我在布局中加载局部视图,Angular JS 将不再在局部视图中工作。我猜我在 DOM 中注入 HTML 后,Angular 引擎没有编译 ng- 指令。

采纳答案by Maarten Bicknese

Instead of inject your HTML manually. Be it through angular.element.html or $().html. Try using one of the following, angular approved, options:

而不是手动注入您的 HTML。无论是通过 angular.element.html 还是 $().html。尝试使用以下经角度批准的选项之一:

  • A routing engine like ngRouteor ui-router. The engine can include a template which is rendered by the backend.
  • The ngIncludedirective. This way you can include HTML templates directly.
  • Write a custom directivewhich injects your HTML.
  • 一个路由引擎,如ngRoute或 ui-router。引擎可以包含由后端呈现的模板。
  • ngInclude指令。这样您就可以直接包含 HTML 模板。
  • 编写一个注入 HTML的自定义指令

Because these are AngularJS friendly methods, Angular will actually parse the HTML and bind any values within the newly loaded HTML.

因为这些是 AngularJS 友好的方法,Angular 实际上会解析 HTML 并绑定新加载的 HTML 中的任何值。

回答by kanchirk

From an answer from here

这里的答案

I think you may need to compile HTML string or DOM into a template.

我认为您可能需要将 HTML 字符串或 DOM 编译成模板。

More information, please refer to:

更多信息,请参考:

$compile

$编译

https://docs.angularjs.org/api/ng/service/$compile

https://docs.angularjs.org/api/ng/service/$compile

There are some threads that may help you:

有一些线程可以帮助您:

AngularJS - Dynamically creating elements that specify directives

AngularJS - 动态创建指定指令的元素

AngularJS - Dynamically creating elements that specify directives

AngularJS - 动态创建指定指令的元素

Bind Angularjs to newly created html element dynamically

将 Angularjs 动态绑定到新创建的 html 元素

Bind Angularjs to newly created html element dynamically

将 Angularjs 动态绑定到新创建的 html 元素