Angularjs 如何将directive controller中定义的范围变量解析为angular js中指令模板中要使用的另一个指令
以下是我试图做的:Angularjs 如何将directive controller中定义的范围变量解析为angular js中指令模板中要使用的另一个指令,angularjs,angularjs-directive,Angularjs,Angularjs Directive,以下是我试图做的: (function () { 'use strict'; var sampleApp = angular.module('sample'); sampleApp.directive('myDirective', sampleAppDirective); function sampleAppDirective () { sampleAppDirectiveController.$inject = ['$scope', '$http', '$attrs', '$ele
(function () {
'use strict';
var sampleApp = angular.module('sample');
sampleApp.directive('myDirective', sampleAppDirective);
function sampleAppDirective () {
sampleAppDirectiveController.$inject = ['$scope', '$http', '$attrs', '$element', '$compile', '$log'];
return {
restrict : 'EA',
controller : sampleAppDirectiveController,
templateUrl : 'partials/sample.html'
};
function sampleAppDirectiveController ($scope, $http, $attrs, $element, $compile, $log) {
$scope.placeholderDir = 'my-other-directive';
}
}})();
partials/sample.html中的模板如下所示
<div><div {{placeholderDir}}></div></div>
是否可以在MyRoller指令中将模板解析为类似的内容
<div><div my-other-directive></div></div>
我想确保我的另一个指令在呈现时也解析为它的模板
请帮忙 是的,但是您需要查看$compile服务 基本上,您需要在指令中生成标记,并使用$compile服务对其进行编译 您可以在链接函数中执行以下操作:
link: function($scope, $element) {
$compile($element.contents())($scope);
}
但我没有测试它
您也可以从DDO中丢弃templateUrl,直接将标记插入元素中:
link: function($scope, $element) {
$element.html('<div><div ' + $scope.placeholderDir + '></div></div>');
$compile($element.contents())($scope);
}
链接:函数($scope,$element){
$element.html(“”);
$compile($element.contents())($scope);
}