Angularjs - 尝试信任需要字符串的内容中的非字符串值:上下文:html - 插入数据时
Angularjs - Attempted to trust a non-string value in a content requiring a string: Context: html - while inserting data
我正在尝试根据 DB 中可用的 ITEMS 动态插入 HTML 内容,并且需要在单击如下动态添加的每个项目的保存按钮时再次保存回 DB。
Controller.js:
function getItemCommentItems() {
$http({
method: 'GET',
url: 'http://xxx/api/ItemComments/GetItemCommentItems',
params: { Area_Id: 'L4' },
headers: { 'Content-Type': 'application/json; charset=utf-8', 'dataType': 'json' }
}).then(function successCallback(response) {
// $scope.itemDtls = [];
$scope.itemDtls = response.data;
displayItems($scope.itemDtls);
}, function errorCallback(response) { });
}
function displayItems(itemData)
{
// alert(itemData.length); Length: 2
for (i = 0; i <= itemData.length; i++) {
Title = '<table><tr><td><label for="ITEM_NAME">Item: </label>{{' & itemData[i].ITEM_NAME & '}}</td></tr ><tr><td><input type="text" id="inpPriority" value="{{ ' & itemData[i].PRIORITY & ' }}" /></td></tr> <tr> <td><input type="text" id="inpComment" value="{{ ' & itemData[i].COMMENT & '}}" /></td></tr><tr> <td><input type="button" ng-click="onSave()" value="Save {{ ' & itemData[i].ITEM_ID & '}}" /></td></tr ></table >';
// Title = $sce.trustAsHtml(itemData[i]); ----> Error here Attempted to trust a non-string value in a content requiring a string: Context: html
$scope.divHtmlVar = $sce.trustAsHtml(Title); ----> Error here Attempted to trust a non-string value in a content requiring a string: Context: html.
}
}
.HTML:
<tr> <td> <div ng-bind-html="divHtml"></div> </td> </tr>
Class 详情:
public string ITEM_ID { get; set; }
public string ITEM_NAME { get; set; }
public string COMMENT { get; set; }
public string PRIORITY { get; set; }
public string ITEM_ID { get; set; }
错误消息:错误:[$sce:type] 试图信任需要字符串的内容中的非字符串值:上下文:html
有人可以帮我解决这个问题,还是有更好的方法来完成动态插入和保存的整个过程?
你为什么首先使用 ng-bind-html
?如果您在模板中描述了您的元素,那就更好了。那么你根本不需要使用 $sce
。我想是这样的:
<table ng-repeat="item in itemDtls">
<tr>
<td>
<label for="ITEM_NAME">Item: </label>{{item.ITEM_NAME}}
</td>
</tr>
<tr>
<td>
<input type="text" id="inpPriority" value="{{item.PRIORITY}}" />
</td>
</tr>
<tr>
<td>
<input type="text" id="inpComment" value="{{item.COMMENT}}" />
</td>
</tr>
<tr>
<td>
<input type="button" ng-click="onSave()" value="Save {{item.ITEM_ID}}" />
</td>
</tr>
</table>
我正在尝试根据 DB 中可用的 ITEMS 动态插入 HTML 内容,并且需要在单击如下动态添加的每个项目的保存按钮时再次保存回 DB。
Controller.js:
function getItemCommentItems() {
$http({
method: 'GET',
url: 'http://xxx/api/ItemComments/GetItemCommentItems',
params: { Area_Id: 'L4' },
headers: { 'Content-Type': 'application/json; charset=utf-8', 'dataType': 'json' }
}).then(function successCallback(response) {
// $scope.itemDtls = [];
$scope.itemDtls = response.data;
displayItems($scope.itemDtls);
}, function errorCallback(response) { });
}
function displayItems(itemData)
{
// alert(itemData.length); Length: 2
for (i = 0; i <= itemData.length; i++) {
Title = '<table><tr><td><label for="ITEM_NAME">Item: </label>{{' & itemData[i].ITEM_NAME & '}}</td></tr ><tr><td><input type="text" id="inpPriority" value="{{ ' & itemData[i].PRIORITY & ' }}" /></td></tr> <tr> <td><input type="text" id="inpComment" value="{{ ' & itemData[i].COMMENT & '}}" /></td></tr><tr> <td><input type="button" ng-click="onSave()" value="Save {{ ' & itemData[i].ITEM_ID & '}}" /></td></tr ></table >';
// Title = $sce.trustAsHtml(itemData[i]); ----> Error here Attempted to trust a non-string value in a content requiring a string: Context: html
$scope.divHtmlVar = $sce.trustAsHtml(Title); ----> Error here Attempted to trust a non-string value in a content requiring a string: Context: html.
}
}
.HTML:
<tr> <td> <div ng-bind-html="divHtml"></div> </td> </tr>
Class 详情:
public string ITEM_ID { get; set; }
public string ITEM_NAME { get; set; }
public string COMMENT { get; set; }
public string PRIORITY { get; set; }
public string ITEM_ID { get; set; }
错误消息:错误:[$sce:type] 试图信任需要字符串的内容中的非字符串值:上下文:html
有人可以帮我解决这个问题,还是有更好的方法来完成动态插入和保存的整个过程?
你为什么首先使用 ng-bind-html
?如果您在模板中描述了您的元素,那就更好了。那么你根本不需要使用 $sce
。我想是这样的:
<table ng-repeat="item in itemDtls">
<tr>
<td>
<label for="ITEM_NAME">Item: </label>{{item.ITEM_NAME}}
</td>
</tr>
<tr>
<td>
<input type="text" id="inpPriority" value="{{item.PRIORITY}}" />
</td>
</tr>
<tr>
<td>
<input type="text" id="inpComment" value="{{item.COMMENT}}" />
</td>
</tr>
<tr>
<td>
<input type="button" ng-click="onSave()" value="Save {{item.ITEM_ID}}" />
</td>
</tr>
</table>