我怎样才能从angularjs渲染DOM对象?

时间:2015-09-09 16:49:23

标签: dom

我通过javascript创建DOM对象,我希望它通过angularjs呈现它,但它显示为[object HTMLDivElement]

但在浏览器控制台中      output of dom object in console

但它呈现出来 output in browser

.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);
        }
    }
}

])

1 个答案:

答案 0 :(得分:2)

在您的控制器中而不是分配DOM元素实例,您可以将$ scope.content设置为html

$scope.content="<div name='ramundra'>ram></div>";