简体   繁体   English

带有模板和控制器的角动态元素

[英]Angular dynamic element with template and controller

I can't understand how to add dynamic content after click on to my HTML with a template file and some controller. 使用模板文件和某些控制器单击我的HTML后,我不明白如何添加动态内容。

I want to something like: 我想要类似的东西:

element.click(function(){
  element2.html(templateUrl, controller);
});

Angular 1.5.8 角度1.5.8

Although the question is not to clear I feel what you want to do is to render an html dynamically. 尽管问题并不清楚,但我认为您想要做的是动态呈现html。 There are two solutions for this: 有两种解决方案:

  1. angular-routing : This is mostly used if you want to navigate to different pages in your application, but you also want the application to be a SPA (Single Page Application), with no page reloading, you can use the ngRoute module. angular-routing :如果您要导航到应用程序中的其他页面,但又希望该应用程序是SPA(单页面应用程序)且无需重新加载页面,则通常使用ngRoute模块,该方法通常用于此。 The content for this is pretty readily available everywhere. 内容随处可见。 You can have a look at them. 你可以看看他们。
  2. custom-directive : You can try to write a directive which would render the html on whichever event you want: custom-directive :您可以尝试编写一个指令,该指令将在所需的任何事件上呈现html:

     .directive("customTemplateLoad", function($http, $templateCache, $compile) { return { restrict: 'A', link: function(scope, element, attr, controller){ var template; // Some logic to get template, perhaps pass it in the directive itself var templatePath = '/templates/'+template; $http.get(templatePath, { cache: $templateCache }).success(function(response) { var contents = element.html(response).contents(); $compile(contents)(scope); }); } };}) 

您可以使用控制器和模板创建指令,对其进行编译,然后将其添加到DOM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM