具有模态的ng模板内的角度指令
Angular directive inside ng-template with modal
我对angular使用预加载指令的方式感到好奇,因为我对驻留在<script>
标记和ng模板中的指令有问题。
当我在chrome dev工具中暂停执行时,在初始文档加载过程中,我可以清楚地看到,如果我的指令位于某个任意模板中,则指令控制器内的代码不会被调用。以下是示例代码,例如myDirective作为myModule.js模块的一部分包含在index.html中时,也包含在索引和主应用程序模块中:
这是其他指令的html,包含有问题的myDirective
<script type="text/ng-template" id="callThis">
<myDirective></myDirective>
</script>`
我用ngDialog这样的点击它
ngDialog.open({
template: 'callThis',
scope: $scope
});
它无法运行该指令,因为它没有任何html可供使用(这就是错误,大约缺少一些html元素)。
最后,这里是包含myDirective的模块的代码
angular.module('myModule', ['myDirectiveTemplates', 'myDirectiveDirective'])
angular.module('myDirectiveTemplates', []).run(["$templateCache", function($templateCache) {$templateCache.put("myDirectiveTemplate.html","<h1></h1>");}]);
angular.module('myDirectiveDirective', []).directive('myDirective', function ($window, $templateCache) {
return {
restrict: 'E',
template: $templateCache.get('myDirectiveTemplate.html'),
controller: function($scope) {
//some arbitrary code
}
};
})
有趣的是,如果我把<my-directive></my-directive>
放在index.html文件中,它就可以正常工作,并且控制器内部的代码会在启动时加载。我不确定如何解决这个问题。
根据我对这个问题的了解,您需要使用$compile服务。这里有一个教程可能会有所帮助:$compile
教程中给出的原因是:
"新铸造的模板没有赋予AngularJS力量然而这就是我们使用$compile服务的地方。。。"
引用自Angular Docs:
将一段HTML字符串或DOM编译为模板,并生成template函数,然后可以使用该函数链接scope和模板在一起。
以下是教程中的一个简短代码示例:
app.directive('contentItem', function ($compile) {
/* EDITED FOR BREVITY */
var linker = function(scope, element, attrs) {
scope.rootDirectory = 'images/';
element.html(getTemplate(scope.content.content_type)).show();
$compile(element.contents())(scope);
}
/* EDITED FOR BREVITY */
});
相关文章:
- 我想使用模态通过php文件发送邮件,并且我希望在提交关闭后关闭pop
- 打开一个模态并将其链接到AngularJS中的指令
- SemanticUI模态not onDeny/onApprove事件未激发
- 有没有比在app.js上绑定模块名称更好的方法来动态加载视图模型和视图以显示模态
- 如何通过所选索引(AngularJS)在模态弹出窗口中显示数据
- 包含数据库中相关信息的开放模态
- bootstrap消除模态并显示另一个模态
- 模态提示-如何重写此代码
- 使用 Angular 和 UI-bootstrap 对模态事件绑定无法工作的指令
- 从指令打开角度模态
- 创建一个模态指令,并将click事件绑定到angular js中
- 模态对话框与指令在角度,范围问题
- 模态窗口中的范围(AngularJS指令)
- 角度:在另一个模态指令中加载的谷歌地图指令保持空白
- 具有模态的ng模板内的角度指令
- 如何在父级$scope、指令$scope和模态$scope之间进行连接
- Angularjs:如何从模态控制器和指令控制器访问主控制器
- Angular UI模态控制器不暴露给指令
- 打开模态时angular js指令出错
- AngularJS自定义Twitter引导模态指令