我正在创建一个选择替换指令,以便根据设计轻松选择样式,而不必总是对一堆标记(即指令为您做到!).
我没有意识到属性不会转移到你放置的位置ng-transclude,只是转到根元素.
我在这里有一个例子:http://plnkr.co/edit/OLLntqMzbGCJS7g7h1j4?p = preview
你可以看到它看起来很棒......但是有一个主要的缺陷.将id和name属性不被转移.其中,你知道,没有name,它没有发布到服务器(这种形式绑定到现有系统,所以AJAXing模型不是一个选项).
例如,这是我开始的:
<select class="my-select irrelevant-class" name="reason" id="reason" data-anything="banana">
<option value="">Reason for Contact...</option>
<option>Banana</option>
<option>Pizza</option>
<option>The good stuff</option>
<option>This is an example of a really, really, really, really, really, really long option item</option>
</select>
Run Code Online (Sandbox Code Playgroud)
......这就是我想要的样子:
<div class="faux-select" ng-class="{ placeholder: default == viewVal, focus: obj.focus }">
<span class="faux-value">{{viewVal}}</span>
<span class="icon-arrow-down"></span>
<select ng-model="val" ng-focus="obj.focus = true" ng-blur="obj.focus = false" ng-transclude class="my-select irrelevant-class" name="reason" id="reason" data-anything="banana">
<option value="">Reason for Contact...</option>
<option>Banana</option>
<option>Pizza</option>
<option>The good stuff</option>
<option>This is an example of a really, really, really, really, really, really long option item</option>
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
......但这是实际发生的事情:
<div class="faux-select my-select irrelevant-class" ng-class="{ placeholder: default == viewVal, focus: obj.focus }" name="reason" id="reason" data-anything="banana">
<span class="faux-value">{{viewVal}}</span>
<span class="icon-arrow-down"></span>
<select ng-model="val" ng-focus="obj.focus = true" ng-blur="obj.focus = false" ng-transclude>
<option value="">Reason for Contact...</option>
<option>Banana</option>
<option>Pizza</option>
<option>The good stuff</option>
<option>This is an example of a really, really, really, really, really, really long option item</option>
</select>
</div>
Run Code Online (Sandbox Code Playgroud)
具体来说,问题是nameselect 上没有属性,所以它实际上并没有将数据发送到服务器.
显然,我可以使用预编译阶段来传输name和id属性(这就是我现在正在做的事情),但如果它只是自动传输所有属性以便它们可以添加任何类,任意数据, (ng-)必需,(ng-)禁用属性等等.
我试着transclude: 'element'开始工作,但后来我无法将模板中的其他属性放到它上面.
注意,我在这里看到了帖子:如何将其转换为属性?,但看起来他们只是手动传输数据,我的目标是让它自动传输所有属性.
您可以使用compile函数来访问元素的属性并构建模板.
app.directive('mySelect', [function () {
return {
transclude: true,
scope: true,
restrict: 'C',
compile: function (element, attrs) {
var template = '<div class="faux-select" ng-class="{ placeholder: default == viewVal, focus: obj.focus }">' +
'<span class="faux-value">{{viewVal}}</span>' +
'<span class="icon-arrow-down entypo-down-open-mini"></span>' +
'<select id="' + attrs.id + '" name="' + attrs.name + '" ng-model="val" ng-focus="obj.focus = true" ng-blur="obj.focus = false" ng-transclude>';
'</select>' +
'</div>';
element.replaceWith(template);
//return the postLink function
return function postLink(scope, elem, attrs) {
var $select = elem.find('select');
scope.default = scope.viewVal = elem.find('option')[0].innerHTML;
scope.$watch('val', function(val) {
if(val === '') scope.viewVal = scope.default;
else scope.viewVal = val;
});
if(!scope.val) scope.val = $select.find('option[selected]').val() || '';
}
}
};
}]);
Run Code Online (Sandbox Code Playgroud)
编译函数返回postLink函数,还有其他方法可以执行此操作,您可以在此处找到更多信息.
| 归档时间: |
|
| 查看次数: |
9519 次 |
| 最近记录: |