绑定到AngularJS数组的元素数组、绑定、元素、AngularJS

由网友(七声颤抖-)分享简介:在AngularJS,我可以成功绑定的文本输入数组的元素:In AngularJS, I can successfully bind text inputs to elements of arrays:这是允许的,还是只是偶然的工作?I...

在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]);
    }
}
阅读全文

相关推荐

最新文章