我通过javascript创建DOM对象,我希望它通过angularjs呈现它,但它显示为[object HTMLDivElement]
.directive('attachmentify', [
function() {
return {
restrict: 'A',
scope: {
item: "@filename"
},
template: "<div ng-bind-html='content'></div>",
compile: function(iElement, iAttrs) {
return function($scope, element, attr) {
var file = $scope.item;
// console.log
// $scope.file =file;
}
},
controller: function($scope) {
var img = ['jpg', 'jpeg', 'png'];
var c = 0;
img.forEach(function(element, index) {
if ($scope.item.endsWith(element)) c++;
});
if (c) {
var div = document.createElement('div');
div.setAttribute('name', 'samundra')
div.innerHTML = "ram"
} else {
var div = document.createElement('div');
div.setAttribute('name', 'ramundra')
div.innerHTML = "sam"
}
$scope.content = div;
console.log(div);
}
}
}
])
答案 0 :(得分:2)
在您的控制器中而不是分配DOM元素实例,您可以将$ scope.content设置为html
$scope.content="<div name='ramundra'>ram></div>";