I am trying to extend the ng-include
directive with this:
我试图用这个扩展ng-include指令:
module.directive('incComp', [function () {
return {
template: "<div ng-include=\"'app/component/{{incComp}}/template.html'\"></div>"
};
}]);
and I use it like this:
我这样使用它:
<div inc-comp="counter"></div>
but the get request that goes out is for:
但是出去的get请求是:
/app/component/%7B%7BincComp%7D%7D/template.html
I'm not sure how to get it insert the value of the inc-comp attribute rather than just using the literal value in the template string. I got the notion of doing this from this SO question (2nd answer).
我不确定如何插入inc-comp属性的值而不是仅仅使用模板字符串中的文字值。我从这个问题得到了这样做的概念(第二个答案)。
2 个解决方案
#1
3
The task can also be done without ng-include and without isolated-scope, a good solution i think:
任务也可以在没有ng-include和没有隔离范围的情况下完成,我认为这是一个很好的解决方案:
plnk演示在这里
index.html
的index.html
<div ng-app="app" ng-controller="appCtrl">
<div add-template type="lion"></div>
<div add-template type="fox"></div>
</div>
<script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.14/angular.js" data-semver="1.3.14"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.directive('addTemplate', function() {
return {
templateUrl: function(elem, attr) {
return 'template_' + attr.type + '.html';
}
}
});
</script>
template_fox.html
template_fox.html
<div> This is Fox Template </div>
template_lion.html
template_lion.html
<div> This is Lion Template </div>
Happy Helping!
快乐帮助!
#2
2
incComp
is evaluated to nothing in your template. You need to add it to your scope. And ng-include takes an expression, not a template. Something like this should work better:
incComp在模板中被评估为空。您需要将其添加到您的范围。并且ng-include采用表达式而不是模板。这样的事情应该更好:
module.directive('incComp', [function () {
return {
scope: true,
controller: function($scope, $element, $attrs) {
$scope.tpl = 'app/component/' + $attrs.incComp + '/template.html'
},
template: "<div ng-include=\"tpl\"></div>"
};
}]);
You could also do that in the link function instead of the controller.
您也可以在链接功能而不是控制器中执行此操作。
I didn't test it though...
我没有测试它...
EDIT: I suppose you could also do something like this:
编辑:我想你也可以这样做:
module.directive('incComp', [function () {
return {
scope: {
incComp: '@'
},
template: "<div ng-include=\"'app/component/' + incComp + '/template.html'\"></div>"
};
}]);
#1
3
The task can also be done without ng-include and without isolated-scope, a good solution i think:
任务也可以在没有ng-include和没有隔离范围的情况下完成,我认为这是一个很好的解决方案:
plnk演示在这里
index.html
的index.html
<div ng-app="app" ng-controller="appCtrl">
<div add-template type="lion"></div>
<div add-template type="fox"></div>
</div>
<script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.14/angular.js" data-semver="1.3.14"></script>
<script type="text/javascript">
var app = angular.module("app", []);
app.directive('addTemplate', function() {
return {
templateUrl: function(elem, attr) {
return 'template_' + attr.type + '.html';
}
}
});
</script>
template_fox.html
template_fox.html
<div> This is Fox Template </div>
template_lion.html
template_lion.html
<div> This is Lion Template </div>
Happy Helping!
快乐帮助!
#2
2
incComp
is evaluated to nothing in your template. You need to add it to your scope. And ng-include takes an expression, not a template. Something like this should work better:
incComp在模板中被评估为空。您需要将其添加到您的范围。并且ng-include采用表达式而不是模板。这样的事情应该更好:
module.directive('incComp', [function () {
return {
scope: true,
controller: function($scope, $element, $attrs) {
$scope.tpl = 'app/component/' + $attrs.incComp + '/template.html'
},
template: "<div ng-include=\"tpl\"></div>"
};
}]);
You could also do that in the link function instead of the controller.
您也可以在链接功能而不是控制器中执行此操作。
I didn't test it though...
我没有测试它...
EDIT: I suppose you could also do something like this:
编辑:我想你也可以这样做:
module.directive('incComp', [function () {
return {
scope: {
incComp: '@'
},
template: "<div ng-include=\"'app/component/' + incComp + '/template.html'\"></div>"
};
}]);