无法在 Angular-Datatables 中使用 ng-model 呈现 <input> 元素
Unable to render <input> element with ng-model in Angular-Datatables
angularjs 1.6.9
解析错误
我正在使用 angularjs 数据表并尝试在呈现的行中包含一个 ng-model 指令,如下所示:
vm.dtColumns = [
DTColumnBuilder.newColumn(null)
.withTitle('')
.notSortable()
.withOption('width', '5%')
.renderWith(function (data, type, full, meta) {
var html = '<input type="checkbox" ng-model="vm.selected[' + data._id + ']"/>';
console.log(html);
return html;
}),
DTColumnBuilder.newColumn('number')
.withTitle('Number')
.withOption('width', '35%'),
DTColumnBuilder.newColumn('name')
.withTitle('Name')
.withOption('width', '50%'),
DTColumnBuilder.newColumn('revisionNumber')
.withTitle('Revision')
.withOption('width', '10%')
];
我得到的是这样的解析异常:
Syntax Error: Token 'a4e90d73bb003d8a9d52b6f' is unexpected, expecting []] at column 14 of the expression [vm.selected[5a4e90d73bb003d8a9d52b6f]] starting at [a4e90d73bb003d8a9d52b6f]].
那 html 行正在对此进行评估:
<input type="checkbox" ng-model="vm.selected[5a4e90d73bb003d8a9d52b6f]"/>
我不跟。我觉得还可以。我该如何解决?
跟进以下答案:
在我使用建议的解决方案之后:
return '<input type="checkbox" ng-model="vm.selected[data._id]"/>';
我的 DOM 看起来像这样:
这使得所有值都分配给同一个变量。对吗?
data._id
生成的字符串需要用引号括在 属性 访问器中:
vm.dtColumns = [
DTColumnBuilder.newColumn(null)
.withTitle('')
.notSortable()
.withOption('width', '5%')
.renderWith(function (data, type, full, meta) {
var accessor = "['" + data._id + "']";
var directive = 'ng-model="selected' + accessor + '"';
var html = '<input type="checkbox" ' + directve + ' />';
console.log(html);
return html;
}),
这将计算为:
̶ ̶<̶i̶n̶p̶u̶t̶ ̶t̶y̶p̶e̶=̶"̶c̶h̶e̶c̶k̶b̶o̶x̶"̶ ̶n̶g̶-̶m̶o̶d̶e̶l̶=̶"̶v̶m̶.̶s̶e̶l̶e̶c̶t̶e̶d̶[̶5̶a̶4̶e̶9̶0̶d̶7̶3̶b̶b̶0̶0̶3̶d̶8̶a̶9̶d̶5̶2̶b̶6̶f̶]̶"̶/̶>̶
<input type="checkbox" ng-model="vm.selected['5a4e90d73bb003d8a9d52b6f']"/>
这假设 vm.selected
被初始化为一个对象。
angularjs 1.6.9
解析错误我正在使用 angularjs 数据表并尝试在呈现的行中包含一个 ng-model 指令,如下所示:
vm.dtColumns = [
DTColumnBuilder.newColumn(null)
.withTitle('')
.notSortable()
.withOption('width', '5%')
.renderWith(function (data, type, full, meta) {
var html = '<input type="checkbox" ng-model="vm.selected[' + data._id + ']"/>';
console.log(html);
return html;
}),
DTColumnBuilder.newColumn('number')
.withTitle('Number')
.withOption('width', '35%'),
DTColumnBuilder.newColumn('name')
.withTitle('Name')
.withOption('width', '50%'),
DTColumnBuilder.newColumn('revisionNumber')
.withTitle('Revision')
.withOption('width', '10%')
];
我得到的是这样的解析异常:
Syntax Error: Token 'a4e90d73bb003d8a9d52b6f' is unexpected, expecting []] at column 14 of the expression [vm.selected[5a4e90d73bb003d8a9d52b6f]] starting at [a4e90d73bb003d8a9d52b6f]].
那 html 行正在对此进行评估:
<input type="checkbox" ng-model="vm.selected[5a4e90d73bb003d8a9d52b6f]"/>
我不跟。我觉得还可以。我该如何解决?
跟进以下答案:
在我使用建议的解决方案之后:
return '<input type="checkbox" ng-model="vm.selected[data._id]"/>';
我的 DOM 看起来像这样:
这使得所有值都分配给同一个变量。对吗?
data._id
生成的字符串需要用引号括在 属性 访问器中:
vm.dtColumns = [
DTColumnBuilder.newColumn(null)
.withTitle('')
.notSortable()
.withOption('width', '5%')
.renderWith(function (data, type, full, meta) {
var accessor = "['" + data._id + "']";
var directive = 'ng-model="selected' + accessor + '"';
var html = '<input type="checkbox" ' + directve + ' />';
console.log(html);
return html;
}),
这将计算为:
̶ ̶<̶i̶n̶p̶u̶t̶ ̶t̶y̶p̶e̶=̶"̶c̶h̶e̶c̶k̶b̶o̶x̶"̶ ̶n̶g̶-̶m̶o̶d̶e̶l̶=̶"̶v̶m̶.̶s̶e̶l̶e̶c̶t̶e̶d̶[̶5̶a̶4̶e̶9̶0̶d̶7̶3̶b̶b̶0̶0̶3̶d̶8̶a̶9̶d̶5̶2̶b̶6̶f̶]̶"̶/̶>̶
<input type="checkbox" ng-model="vm.selected['5a4e90d73bb003d8a9d52b6f']"/>
这假设 vm.selected
被初始化为一个对象。