gpt4 book ai didi

angularjs - 在 ng-repeat 中动态添加指令

转载 作者:行者123 更新时间:2023-12-03 06:26:54 25 4
gpt4 key购买 nike

我正在尝试在 ng-repeat 中动态添加不同的指令,但是输出不会被解释为指令。

我在这里添加了一个简单的示例:http://plnkr.co/edit/6pREpoqvmcnJJWzhZZKq

Controller :

$scope.colors = [{name:"red"}, {name: "blue"}, {name:"yellow"}]; 

指令:

app.directive("red", function () {
return {
restrict: 'C',
template: "RED directive"
}
});

HTML:

<ul>
<li ng-repeat="color in colors">
<span class="{{color.name}}"></span>
</li>
</ul>

如何让 Angular 拾取通过 ng-repeat 输出的 class 中指定的指令?

最佳答案

我知道这是一个老问题,但是谷歌把我带到了这里,我不喜欢这里的答案......对于应该简单的事情来说,它们看起来真的很复杂。所以我创建了这个指令:

***** 新内容 *****

此后,我使该指令更加通用,支持解析的(典型的 Angular 值)“attributes”属性。

/**
* Author: Eric Ferreira <http://stackoverflow.com/users/2954747/eric-ferreira> ©2016
*
* This directive takes an attribute object or string and adds it to the element
* before compilation is done. It doesn't remove any attributes, so all
* pre-added attributes will remain.
*
* @param {Object<String, String>?} attributes - object of attributes and values
*/
.directive('attributes', function attributesDirective($compile, $parse) {
'use strict';

return {
priority: 999,
terminal: true,
restrict: 'A',
compile: function attributesCompile() {
return function attributesLink($scope, element, attributes) {
function parseAttr(key, value) {
function convertToDashes(match) {
return match[0] + '-' + match[1].toLowerCase();
}

attributes.$set(key.replace(/([a-z][A-Z])/g, convertToDashes), value !== undefined && value !== null ? value : '');
}

var passedAttributes = $parse(attributes.attributes)($scope);

if (passedAttributes !== null && passedAttributes !== undefined) {
if (typeof passedAttributes === 'object') {
for (var subkey in passedAttributes) {
parseAttr(subkey, passedAttributes[subkey]);
}
} else if (typeof passedAttributes === 'string') {
parseAttr(passedAttributes, null);
}
}

$compile(element, null, 999)($scope);
};
}
};
});

对于OP的用例,你可以这样做:

<li ng-repeat="color in colors">
<span attributes="{'class': color.name}"></span>
</li>

或者将其用作属性指令:

<li ng-repeat="color in colors">
<span attributes="color.name"></span>
</li>

***** 结束新内容 *****

/**
* Author: Eric Ferreira <http://stackoverflow.com/users/2954747/eric-ferreira> ©2015
*
* This directive will simply take a string directive name and do a simple compilation.
* For anything more complex, more work is needed.
*/
angular.module('attributes', [])

.directive('directive', function($compile, $interpolate) {
return {
template: '',
link: function($scope, element, attributes) {
element.append($compile('<div ' + attributes.directive + '></div>')($scope));
}
};
})

;

对于这个问题中的具体情况,只需稍微重写该指令,使其按类将该指令应用于跨度,如下所示:

angular.module('attributes', [])

.directive('directive', function($compile, $interpolate) {
return {
template: '',
link: function($scope, element, attributes) {
element.replaceWith($compile('<span class=\"' + attributes.directive + '\"></span>')($scope));
}
};
})

;

然后您可以在任何地方使用它并按名称动态选择指令。像这样使用它:

<li ng-repeat="color in colors">
<span directive="{{color.name}}"></span>
</li>

我故意让这个指令简单明了。您可能(并且可能会)必须重新措辞以满足您的需求。

关于angularjs - 在 ng-repeat 中动态添加指令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16264534/

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