绑定到AngularJS数组的元素 [英] Binding to Elements of Arrays in AngularJS

查看:577
本文介绍了绑定到AngularJS数组的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AngularJS,我可以成功绑定的文本输入数组的元素:

In AngularJS, I can successfully bind text inputs to elements of arrays:

<input type="text" ng-model="foo[2]" />

这是允许的,还是只是偶然的工作?

Is this allowed, or does it just work by accident?

当我尝试绑定选择元素或复选框输入数组元素,他们无法正常工作 - 即选择元素不上选择改变显示的,或约束,价值和点击时复选框不显示打勾。

When I try to bind select elements or checkbox input to array elements they fail to work - i.e. the select element does not change the displayed, or bound, value on selection and the checkbox does not display a tick when clicked.

我失去了一些东西在这里?

Am I missing something here?

更新:它的工作原理的jsfiddle: http://jsfiddle.net/azFzc/

Update: It works in JSFiddle: http://jsfiddle.net/azFzc/

推荐答案

它确实在特定元素的工作。

It does work on select elements

看这个的jsfiddle http://jsfiddle.net/fStE7/

Look this jsfiddle http://jsfiddle.net/fStE7/

HTML

<div ng-app>
<div ng-controller="MyController">
    Select
<select ng-change="changed()" 
        ng-options="option.value as option.label for option in options" ng-model="ar[0]">
 </select>
</div>
</div>

JS

function MyController($scope){
    $scope.options = [
        { label : "first element", value : 0 },
        { label : "second element", value : 1 },
    ]

    $scope.ar = [];
    $scope.ar[0] = 0;

    $scope.changed = function(){
         console.log($scope.ar[0]);
    }
}

这篇关于绑定到AngularJS数组的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆