将占位符添加到角度 {{}}
Add a placeholder to angular {{}}
我得到了这个可编辑的指令,它工作得很好,除了在模型包含数据之前它不会显示文本。
我尝试过的方法如下:
{{option.title | Test}}
我希望这要么显示option.title
,但事实并非如此。
以下是 HTML 代码:
<editable model="option.title">{{option.title | Test}}</editable>
我可以使用ng-if
吗?
这是指令:
App.directive('editable', function() {
return {
restrict: 'E',
scope: {model: '='},
replace: false,
template:
'<span>'+
'<input type="text" ng-model="model" style="width: 100%; font-size: 18px" ng-show="edit" ng-enter="edit=false"></input>'+
'<span ng-show="!edit">{{model}} <i class="fa fa-pencil" style="font-size:20px;"></i></span>'+
'</span>',
link: function(scope, element, attrs) {
scope.edit = false;
element.bind('click', function() {
scope.$apply(scope.edit = true);
element.find('input').focus();
});
}
};
});
编辑 1
试试这个:
App.directive('editable', function($compile) {
return {
restrict: 'E',
scope: {model: '='},
replace: false,
template:
$compile('<span>'+
'<input type="text" ng-model="model" style="width: 100%; font-size: 18px" ng-show="edit" ng-enter="edit=false"></input>'+
'<span ng-show="!edit">{{model}} <i class="fa fa-pencil" style="font-size:20px;"></i></span>'+
'</span>'),
link: function(scope, element, attrs) {
scope.edit = false;
element.bind('click', function() {
scope.$apply(scope.edit = true);
element.find('input').focus();
});
}
};
});
编辑 2
尝试将变量初始化为:
App.directive('editable', function($compile) {
return {
restrict: 'E',
scope: {model: '='},
replace: false,
template:
$compile('<span>'+
'<input type="text" ng-model="model" style="width: 100%; font-size: 18px" ng-show="edit" ng-enter="edit=false"></input>'+
'<span ng-show="!edit">{{model}} <i class="fa fa-pencil" style="font-size:20px;"></i></span>'+
'</span>'),
link: function(scope, element, attrs) {
scope.edit = false;
scope.model = '';
element.bind('click', function() {
scope.$apply(scope.edit = true);
element.find('input').focus();
});
}
};
});
编辑 3
尝试:
<editable ng-init="option={title:''};" model="option.title">{{option.title | Test}}</editable>
如果您不使用 tansclusion,指令的开始标记和结束标记之间的内容将完全替换为指令的模板。
<editable model="option.title">{{option.title || 'Placeholder Text' | Test}}</editable>
-
{{model || 'text before model updates' | filter}}
相关文章:
- 当用户在输入字段事件中输入内容时,为该字段事件添加动画占位符
- 如何将占位符属性添加到 CKEditor 的实例
- 角度 - 需要向选择下拉列表添加多个占位符
- 添加一些值,以便在用户更改该服务器时将其作为占位符输入并保留它
- 使用 PURE JS 基于输入标签添加占位符属性 - 没有 jQuery
- Javascript:如何修改我的代码以在主题发布后添加回消失的占位符
- 输入字段html5占位符没有'不要在javascript中添加值后就消失
- 如何在jQuery数据表上添加带有搜索图标的占位符文本
- 在角度中向{{}}添加占位符
- 将占位符添加到角度 {{}}
- 如何为可拖动项添加占位符
- 如何编写一行可以添加到showpic函数末尾的代码,该函数更新占位符元素上的alt属性
- 向LI添加占位符attr
- 在.hbs ember模板文件的占位符中添加图像
- Javascript -在自动完成结果的点击上添加永久占位符
- 如何在编辑器中动态添加占位符
- 将占位符添加到ng网格中
- 删除小屏幕/移动设备上的输入占位符,但在调整为桌面大小时再次添加它们
- 如何向 jQuery Tokeninput 添加占位符
- CSS是否有办法在光标和输入中的占位符文本之间添加空格?