博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to dynamically load directive into page
阅读量:5843 次
发布时间:2019-06-18

本文共 2910 字,大约阅读时间需要 9 分钟。

I have an html file with a controller and a directive with a template url. I want to load/compile the directive conditionally in the controller:

Controller:

app.controller('TestController', function TestController($http, $scope, $compile) { $scope.loadData = function (pageId) { var pUrl = 
$http({ method: 'GET', url: pUrl }).success(function (data, status) { $scope.pData = data; var htm = '
'; var elm = angular.element("#id").append(htm); $compile(elm)($scope); }).error(function (data, status) { alert('error'); }); }; $scope.loadData(); });

Directive:

'use strict';app.directive('testdirective', function ($http) { var uDirective = {}; uDirective.restrict = 'E'; uDirective.templateUrl = 'js/directives/testdirective.html'; uDirective.controller = function ($scope, $element, $attrs) { $scope.showDirectiveData(); $scope.showDirectiveData = function () { $scope.directiveDataCollection = 
; }; }; uDirective.compile = function (element, attributes) { // do one-time configuration of element. var linkFunction = function ($scope, element, atttributes) { }; return linkFunction; }; return uDirective; });

Template used in Directive

{ {directiveData.Title}}

How do i get to compile the code in the TestController, load the directive dynamically, and finally load the content and append the content in scope?

--------------------------------------------------------------------------------------------------------------------------------------------------

Here is a general template for you to reference that abstracts and also demonstrates a few Angular concepts:

JS

.directive('parentDirective', function(Resource, $compile){ return { restrict: 'E', link: function(scope, elem, attrs){ Resource.loadData().then(function(result){ scope.data = result.data; var htm = '
'; var compiled = $compile(htm)(scope); elem.append(compiled); }); } } }) .directive('childDirective', function(){ return { restrict: 'E', template: '
Content: { {data.key}}
' } }) .factory('Resource', function($http){ var Resource = {}; Resource.loadData = function(){ return $http.get('test.json'); } return Resource; })

HTML

 

Notice that there is no controller code. This is because controllers should never manipulate the DOM- one reason is that it will make your code a PITA to test. So, I put everything in directives, where it should probably be in your case as well.

I also moved the $http service into a factory. Anything state/model related should be in a service. Among other reasons, by doing this, you can inject it almost anywhere (including inside of directives) to access your data without worrying about it disappearing when a controller unloads.

EDIT

You should also consider the dissenting view of the dynamic loading approach in general in the accepted answer of 

 

转载地址:http://lkqcx.baihongyu.com/

你可能感兴趣的文章
Mac os 10.9 Python MySQLdb
查看>>
理解对象(通过关联数组和基本包装类型)
查看>>
linux查看系统版本(32位/64位)的方法
查看>>
linux基础--awk文本分析工具详解
查看>>
Highcharts中Legend动态显示点值
查看>>
结合bgp路由反射器和internet访问的mpls *** 实验
查看>>
MongoDB笔记五——插入操作
查看>>
我的友情链接
查看>>
bash脚本示例1
查看>>
企业应用系统驱动企业业务变革
查看>>
mysql(三)
查看>>
MySQL数据库主从同步(单台2实例)
查看>>
java中按字节获得字符串长度的两种方法 Java问题通用解决代码
查看>>
render: h => h(App) $mount 什么意思
查看>>
HashMap和HashTable简介和区别
查看>>
java json 库之 jackson
查看>>
【图像缩放】最邻近插值
查看>>
一个关于对象引用的bug引发的对于引用类型及数组的简单思考
查看>>
JavaScript 进阶知识 - 特效篇(一)
查看>>
React Native+Node.js 开发的课程表app项目笔记
查看>>