我有一個由ng-repeat呈現並使用ng-bind綁定到模型的表。我想根據條件在第一列添加一個html元素。我怎樣才能做到這一點?在ng-bind中添加內容之前的html元素
<!doctype html>
<html ng-app="plunker">
<head>
<script data-require="[email protected]*" data-semver="1.2.0" src="http://code.angularjs.org/1.2.0/angular.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body>
<div ng:controller="MainCtrl">
<table border="1">
<thead style="font-weight: bold;">
<tr>
<th class="text-right" ng-repeat="column in columnsTest" ng-if="column.checked" ng-bind="column.id"></th>
</tr>
</thead>
<tbody>
<tr ng-repeat="row in rows">
<td ng-repeat="column in columnsTest" ng-if="column.checked" ng-bind="formatValue(row[column.id], column.id == 'Value1')"></td>
</tr>
</tbody>
</table>
</div>
<script>
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, $filter) {
$scope.formatValue = function(value, addIcon) {
if (addIcon) {
var htmlElement = '<span>This is a SPAN</span>'
value = htmlElement + value;
}
return value;
}
$scope.columnsTest = [{
id: 'Value1',
checked: true
}, {
id: 'Value2',
checked: true
}, {
id: 'Value3',
checked: true
}];
$scope.rows = [{
id: 1,
"Value1": 911,
"Value2": 20,
"Value3": 20
}, {
id: 2,
"Value1": 200,
"Value2": 20,
"Value3": 20
}];
});
我想我的HTML元素的值之前被渲染。我試過使用$範圍但這並不奏效。該跨度仍然是一個文本。
這裏是一個Plunker
非常感謝您更好的渲染性能。我不知道ng-bind會在td元素 – MrProgram
內產生與wrint {{}}相同的結果。歡迎您。是的,ngBind基本上是'{{}}'標籤的屬性版本。 – dfsq