【发布时间】:2014-12-16 16:30:18
【问题描述】:
好的,我得到了一个页面,其中包含一些使用 angular x-editable 管理的输入。
以下指令:
treeView.directive('selectWhenEditing',function(){
var linkFunction = function(scope,startingelem,attr)
{
console.log(startingelem[0]);
console.log (startingelem[0].querySelector('.editable'));
angular.element(startingelem[0].querySelector('.editable')).on('click',function(){
//console.log(startingelem[0]);
angular.element(startingelem[0].querySelector('.editable-input').select());
});
};
return{
restrict: 'AEC',
link:linkFunction
};}
);
当我单击可编辑元素时,应该会选择文本。
这是页面的代码:
<tr ng-repeat="properties in elementToEdit" ng-if="fieldsSettings[$index][1]==1">
<td>
{{ fieldsSettings[$index][0] }}
</td>
<td data-select-when-editing>
<span editable-text="properties.prop" onbeforesave="" ng-if="fieldsSettings[$index][2]== 'text'">
{{ properties.prop || 'empty' }}
</span>
<span editable-select="properties.prop" onbeforesave="" ng-if="fieldsSettings[$index][2]== 'select'" e-ng-options="s.value as s.text for s in fieldsSettings[$index][3]">
{{ selectElement(properties.prop,$index) || 'empty' }}
</span>
</td>
</tr>
这就是相关的编译 HTML:
<tr class="ng-scope" ng-if="fieldsSettings[$index][1]==1" ng-repeat="properties in elementToEdit">
<td class="ng-binding"> Name </td>
<td data-select-when-editing="">
<span class="ng-scope ng-binding editable editable-click" ng-if="fieldsSettings[$index][2]== 'text'" onbeforesave="" editable-text="properties.prop"> Food </span>
</td>
</tr>
该指令按我的意愿触发(例如:期望它的次数)。 第一个 console.log 按预期记录了调用 TD 元素。但是第二个是空的,因为它永远不会在这个 TD 中找到元素“.editable”,所以显然永远不会选择文本(不应用事件“on”)。
我在另一个页面/模块中做了完全相同的事情,而且效果很好。我正在比较代码,但找不到差异。
你们看到了什么?
【问题讨论】:
-
建议利用
jQlite,它使用jQuery方法语法和startingelem.find('.editable')。无需创建新的angular.element,因为startingelem已经是一个 -
好的,谢谢……现在它确实找到了一个元素,但是当我在它上面应用“点击”事件时……我明天早上再试一次,已经晚了:)
-
好的,我实际上正在这样做,它会找到一个元素,但是如果我这样做,则开始elem.find('.editable').text() 控制台是“空字符串”,事实并非如此。我不确定它是否选择了正确的东西......
-
创建一个复制您的问题的演示
-
jsfiddle.net/Morgorth/xezbs8gn 这是小提琴,有点简化,但在控制台中得到相同的结果
标签: javascript angularjs angularjs-directive x-editable