我在使用 replace: true 的自定义指令时遇到问题,
http://jsbin.com/OtARocO/2/edit
据我所知,我只有一个根元素 my ,这是怎么回事?
Error: Template must have exactly one root element. was:
<tbody>
<tr><td>{{ item.name }}</td></tr>
<tr><td>row2</td></tr>
</tbody>
Javascript:
var app = angular.module("AngularApp", [])
.directive('custom', [function () {
return {
restrict: 'E',
replace: true,
templateUrl: 'lineItem.html',
link: function(scope, element, attrs) {
}
};
}])
.controller('MyCtrl', ['$scope', function($scope) {
$scope.items = [
{
name: 'foo'
},
{
name: 'bar'
},
{
name: 'baz'
}
];
}]);
HTML:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js"></script>
<meta name="description" content="Angular Avatar Example" />
<script src="//crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<meta charset=utf-8 />
<title>JS Bin</title>
</head>
<body data-ng-app="AngularApp">
<script type="text/ng-template" id="lineItem.html">
<tbody>
<tr><td>{{ item.name }}</td></tr>
<tr><td>row2</td></tr>
</tbody>
</script>
<div data-ng-controller="MyCtrl">
<table>
<custom data-ng-repeat="item in items"></custom>
</table>
</div>
</body>
</html>