gpt4 book ai didi

javascript - 指令不生成模板

转载 作者:行者123 更新时间:2023-11-29 19:16:43 25 4
gpt4 key购买 nike

var app = angular.module('app',[]);

app.directive('myGridTable',function($http){
var tbl_tpl = '<table>';

return {
restrict:'AE',
template:'{{tbl_tpl}}',
replace:true,
link:function(scope,element,attrib){
$http.get(attrib.src).success(function(response) {
scope.rows = response.data;
//rows and columns population....
angular.forEach(scope.rows,function(value,key){
console.log(value);
tbl_tpl = tbl_tpl + "<tr>";
angular.forEach(value,function(val,k){
tbl_tpl=tbl_tpl + "<td>" + val + "</td>" ;
});
tbl_tpl = tbl_tpl + "</tr>";
});
tbl_tpl = tbl_tpl + "</table>";
scope.tbl_tpl=tbl_tpl;
});
}

};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

我的查询是如果替换关闭则“tbl_tpl”生成纯文本否则显示错误....因此我有办法解决这个问题

最佳答案

最简单的方法就是使用一个指令。无需复杂化 Controller 等。

指令:

.directive('tableList', function($http) {
return {
restrict:'E',
template: '<table><tr ng-repeat="row in rows"><td>{{row.property}}</td></tr></table>',
link:function(scope,element,attr){
scope.rows = [];
$http.get('/api/rows').then(function(response) {
scope.rows = response.data;
});
}
};
});

然后在你的模板中使用指令:

<table-list></table-list>

关于javascript - 指令不生成模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34776179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com