1

我有这段代码,它是 CraigStuntz 的内容可编辑占位符。

我的问题是我应该把这段代码放在哪里,这样所有div包含的内容contenteditable都会生效。

我试图把它放在下面,app.controller('myCtrl', function(){});但它只适用于直接作用域。没有嵌套范围有效。

angular.element(document.querySelectorAll('div[contenteditable]')).bind('change keydown keypress input', function() {
    if (this.textContent) {
        this.setAttribute('data-contenteditable-placeholder', 'true');
    }
    else {
        this.removeAttribute('data-contenteditable-placeholder');
    }
});
4

2 回答 2

4

尝试使用这样的指令。

angular.module('angularProject.directives', [])
.directive('placeholder', function() {
    return {
        restrict:'A',
        link: function(elem) {
            elem.bind('change keydown keypress input', function() {
                elem.setAttribute('data-contenteditable-placeholder', 'true');
            });
        }
    }
});

然后,您可以像这样将它放在任何 div 中。

<div placeholder></div>

我没有测试过这个。

于 2013-08-01T16:09:04.510 回答
1

我建议您可以为此任务创建一个指令

于 2013-08-01T14:28:18.650 回答