gpt4 book ai didi

angularjs - 在 AngularJS 指令中嵌入属性

转载 作者:行者123 更新时间:2023-12-04 02:21:13 24 4
gpt4 key购买 nike

我正在创建一个选择替换指令,以便根据设计轻松设置选择的样式,而不必总是纠正一堆标记(即指令为您完成!)。

我没有意识到属性不包含在您放置的位置 ng-transclude只需转到根元素。

我这里有一个例子:http://plnkr.co/edit/OLLntqMzbGCJS7g7h1j4?p=preview

你可以看到它看起来很棒......但是有一个主要缺陷。 idname属性不会被转移。哪个,你知道,没有 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>

...这就是我想要的样子:
<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>

...但这就是实际发生的情况:
<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>

具体来说,问题是没有 name属性,所以它实际上并没有将数据发送到服务器。

显然,我可以使用预编译阶段来传输 nameid属性(这就是我现在正在做的事情),但如果它能够自动传输所有属性以便他们可以添加任何类、任意数据、(ng-)必需、(ng-)禁用属性等,那就太好了, ETC。

我试着得到 transclude: 'element'工作,但后来我无法将模板中的其他属性添加到它上面。

注意,我在这里看到了帖子: How can I transclude into an attribute? ,但看起来他们只是手动传输数据,我的目标是让它自动传输所有属性。

最佳答案

您可以使用 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() || '';
}
}
};
}]);

compile 函数返回 postLink 函数,还有其他方法可以做到这一点,你会发现更多信息 here .

Here is a plunker

关于angularjs - 在 AngularJS 指令中嵌入属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18804438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com