- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个指令来加载自定义模板,但如果自定义模板不存在,则加载默认模板。
这是我的代码:
HTML:
<my-include src="path/to/template.html"></my-include>
指令:
angular.module('app')
.directive("myInclude", function ($compile) {
return {
restrict: 'CAE',
replace: true,
scope: {
src: '@',
},
link: function (scope, iElement, iAttrs, controller) {
scope.$on("$includeContentError", function (event, args) {
scope.src = args.replace('/custom', '').replace("'", '');
});
scope.$on("$includeContentLoaded", function (event, args) {
scope.src = args.replace("'", '');
});
},
template: '<div class="include-container" ng-include="src"></div>'
};
})
;
我遇到的问题是...我的模板没有显示...当我调试它时,它会转到指令并替换 src。但我得到的 html 如下:
<div class="include-container ng-scope" ng-include="src" src="path/to/template.html"><div class="main-information-content ng-scope">
</div>
知道如何解决这个问题吗?我猜这是因为“ng-include='src'”,其中“src”没有被路径替换...如何修复它?
编辑:
我尝试放置此模板:
template: '<div class="include-container" ng-include="{{ src }}"></div>'
但我收到此错误:
Error: [$parse:syntax] http://errors.angularjs.org/1.5.0/$parse/syntax?p0=%7B&p1=invalid%20key&p2=2&p3=%7B%7Brc%20%7D%7D&p4=%7B%src%20%7D%7D
编辑2:当我把它作为模板时:
template: '<div class="include-container" ng-include="tmpSrc"></div>'
并用 scope.tmpSrc
替换 scope.src
,ng-include 值现在很好,但是我的 html View 中替换的模板被注释掉了......为什么?
编辑3:
使用您的代码片段,这是我需要做的事情的想法:
angular
.module('app', [])
.directive("myInclude", function($compile) {
return {
restrict: 'CAE',
replace: true,
scope: {
src: '@',
},
link: function(scope, iElement, iAttrs, controller) {
scope.$on("$includeContentError", function() {
scope.src = 'error-template.html';
});
scope.$on("$includeContentLoaded", function(event, args) {
// No need to do anything - the content has loaded.
});
},
template: '<div class="include-container" ng-include="src"></div>'
};
})
.controller('mainController', ['$scope', '$http',
function($scope, $http) {
// Is the content loaded ?
$scope.state = 'loading';
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<div ng-app="app">
<div>
This will show the correct template:
<my-include src="path/to/template.html"></my-include>
</div>
<div>
This will show an error template:
<div ng-controller="mainController as main">
<my-include src="something/that/does/not/exist.html"></my-include>
</div>
</div>
<script type="text/ng-template" id="path/to/template.html">
<h1>I want to display state : {{ state }}</h1>
</script>
<script type="text/ng-template" id="error-template.html">
<h1>Hello from "error-template.html"</h1>
</script>
</div>
最佳答案
基本上,您想要的是“增强” ngInclude 以支持和错误回退,而不是创建新范围(因为这可能会导致 scope's prototypical inheritance 出现某些“错误”,例如 this one 或 this one等)。
我创建了一个执行此操作的指令。它加载通过 src 属性指定的自定义模板,并支持通过 error-src 属性指定的后备模板选项。
我并不是很喜欢这种方法,特别是如果您在模板中添加依赖于其父级的逻辑。您应该将模板逻辑委托(delegate)给集中且可重用的指令。这将有助于测试过程并隐藏实现细节。
angular
.module('app', [])
.controller('MainController', ['$scope',
function($scope) {
// Is the content loaded ?
$scope.state = 'loading';
}
])
.directive('staticNgInclude', ['$compile', '$http', '$templateCache',
function($compile, $http, $templateCache) {
return {
link: function(scope, iElement, iAttrs) {
if (angular.isUndefined(iAttrs.src)) {
throw 'staticNgInclude requires the src attribute.'
}
$http
.get(iAttrs.src, {
cache: $templateCache
}).then(function(response) {
// Hooray, the template was found!
$compile(iElement.html(response.data).contents())(scope);
}, function() {
// Fetch the error template!
$http
.get(iAttrs.errorSrc || 'error-template.html', {
cache: $templateCache
}).then(function(response) {
$compile(iElement.html(response.data).contents())(scope);
});
});
},
replace: false,
restrict: 'E'
};
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="MainController">
This will show the correct template:
<static-ng-include src="path/to/template.html"></static-ng-include>
</div>
<div>
This will show an error template:
<static-ng-include src="something/that/does/not/exist.html"></static-ng-include>
</div>
<script type="text/ng-template" id="path/to/template.html">
<h1>I want to display state : {{ state }}</h1>
</script>
<script type="text/ng-template" id="error-template.html">
<h1>Hello from "error-template.html"</h1>
</script>
</div>
关于javascript - 自定义 ngInclude : variable in template not replaced,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35843241/
template struct List { }; template class> struct ListHelper; template struct ListHelper> { };
最近,我注意到 html/template.Template 的 Templates() 与 text/template.Template 的工作方式不同。 // go1.12 func main()
我正在尝试使用 polymer 1.0 实现一个网站。我有一个自定义元素 my-greeting,里面有一些模板重复。 我想做的是获取一个名为 TARGET 的字符串,但我不知道该怎么做: /cons
(是的,由于我糟糕的英语,标题很奇怪;我希望有人能改进它。) 接听this question ,我发现这段代码有效: template class A { }; template class U>
这个问题在这里已经有了答案: How to import and use different packages of the same name (2 个答案) 关闭 4 年前。 我正在使用 Go
我的想法是这是不可能的,或者我缺少一个额外的步骤。无论哪种方式,我都被卡住了,无法弄清楚。 使用内联模板的原因是能够使用 Laravel Blade 语法并结合 Vue Js 的强大功能。似乎是两者中
我已经尝试实现一个“模板模板模板”——模板类来满足我的需求(我对使用模板元编程很陌生)。不幸的是,我发现以下主题为时已晚: Template Template Parameters 不过,我需要实现如
Helm _helpers.tpl? Helm 允许使用 Go templating在 Kubernetes 的资源文件中。 一个名为 _helpers.tpl 的文件通常用于定义 Go 模板助手,语
{{template "base"}} 和 {{template "base".}} 有什么区别? 我用的是go-gin,两者都可以正常运行。我在文档中找不到关于此的任何描述。 最佳答案 来自 god
我有一个本质上充当查找表的函数: function lookup(a::Int64, x::Float64, y::Float64) if a == 1 z = 2*x + y else if a =
当 out 成员函数(来自模板和特化)都需要模板时,为什么 c++ 需要模板参数,因为我没有得到它,谷歌也没有帮助。必须是c++11但和c++1z有同样的错误。 我正在使用 g++ 7.3.0 收到此
我正在寻找简单的方法来将带有 ${myvar} 的简单模板转换为带有 {{ myvar }} 的 GO 模板。 是否有任何库可以实现这一点? 最佳答案 使用正则表达式查找 \${([a-z0-9\_\
我有这个模板可以将 slice 的多个项目解析到页面上。它确实做得很好。 但是,我现在想使用完全相同的模板来根据范围索引解析 slice 的单个值。该 slice 在多个文件中使用,所以我不能像 Sl
要清理模板文件夹,我想将常用模板保存在子文件夹中。目前我有以下文件结构: main.go templates/index.tpl # Main template for the
最近我设计了元类型和允许编译时类型连接的可能操作: #include template typename T> struct MetaTypeTag {}; /*variable template
准备模板时发生错误。谁能告诉你怎么修? 如有必要,还可以编辑变量。 vars: AllСountry: - "name1" - "name2"
我在使用新的匿名模板引擎时遇到问题。它不能使用嵌套模板。我收到错误消息:“此模板引擎不支持嵌套在其模板中的匿名模板”。 我的问题:我如何强制 knockout JS 使用jquery 模板引擎,而不是
这个问题在这里已经有了答案: Where and why do I have to put the "template" and "typename" keywords? (8 个答案) 关闭 8
我在 C++ 中使用带有模板的集合: template class OMSSVDisk : public OMSSObjProperties{ set memberPDs; }; 如上面代码中
因为我喜欢分离接口(interface)和实现,而不是只在头文件中实现模板类,我将它分成 .h 和 .tpp(.tpp 这样它就不会用 *.cpp 编译)。然后我将 tpp 包含在头文件的末尾,就在
我是一名优秀的程序员,十分优秀!