gpt4 book ai didi

javascript - 通过 Angular 指令在 html 模板中迭代配置数组?

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:59:06 26 4
gpt4 key购买 nike

我最近开始使用 AngularJS 并尝试创建一个注册表单,我会从我的 JavaScript 配置中填写国家/地区列表,该配置在其他地方广泛使用,因此我将其保留为 JS 对象。

我一直在尝试在我的选择输入上使用 ng-repeat-start 和 *-end,但它失败了。

主要问题是,如何加载国家/地区数组并在我的模板中对其进行迭代?

编辑:2014 年 11 月 30 日 - 更好的例子

HTML:

<div class="form-group">
<label for="phone">Country</label>
<select-country ng-model="credentials.country"></select-country>
<pre>{{credentials.country}}</pre>
</div>

文件:

/public/directives/countrySelector.directive.js

指令内容:

'use strict';

angular.module('app', [])
.value('countries', [
{name: 'Afghanistan', code: 'AF'},
{name: 'Åland Islands', code: 'AX'}
])
.directive('selectCountry', ['countries', function (countries) {
function link (scope, element, attrs) {
scope.countries = countries;
}

return {
template: '<select ng-options="country[1] as country[0] for country in countries"' +
' ng-model="credentials.country">' +
'</select>',
link: link,
scope: {
credentialsCountry: '='
}
};
}]);

最佳答案

只需将 country[0]country[1] 替换为 country.codecountry.name

http://plnkr.co/edit/OIGGitze5LLehDes2MQ8?p=preview或者我错过了什么?

关于javascript - 通过 Angular 指令在 html 模板中迭代配置数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26958312/

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