ng-repeat中的Angular JS绑定范围数据以形成 [英] Angular JS binding scope data within ng-repeat to form

查看:30
本文介绍了ng-repeat中的Angular JS绑定范围数据以形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 $scope.data 中有一个项目集合,其中包含字段id"、name"和age",使用 ng-repeat 指令显示在视图中.对于每组项目,都有一个对应的编辑按钮".

我希望能够访问按下编辑按钮的特定项目集的值.

HTML:

<div ng-repeat="i 在数据中">姓名:{{i.name}}年龄:{{i.age}}<form ng-submit="submit()"><input type="text" ng-model="i.id"/><input type="submit" value="编辑"/></表单>

脚本:

函数 Ctrl($scope){$scope.data = [{id:1,name:"Alex",age:22},{id:2,name:"Sam", age:28}];$scope.submit = function() {//点击编辑的用户的访问ID};}

这样做的正确方法是什么?

解决方案

试试这个:

HTML:

JavaScript:

$scope.submit = function(userId) {//你有用户 ID};

I have a collection of items in $scope.data with fields "id","name" & "age", that are being displayed in the view using ng-repeat directive. For each set of items, there is a corresponding "edit button".

I want to be able to access values for the particular set of items for which edit button was pressed.

Html:

<div ng-controller="Ctrl">
    <div ng-repeat="i in data">
        Name: {{i.name}}
        Age: {{i.age}}

        <form ng-submit="submit()">
            <input type="text" ng-model="i.id"/>
            <input type="submit" value="Edit" />
        </form>
    </div>
</div>

Script:

function Ctrl($scope) 
{
  $scope.data = [
    {id:1,name:"Alex",age:22},
    {id:2,name:"Sam", age:28}
    ];

    $scope.submit = function() {
        //access id of user for which edit was clicked
    };
}

What is the right way to do this?

解决方案

Try this:

HTML:

<form ng-submit="submit(i.id)">

JavaScript:

$scope.submit = function(userId) {
  // you have userId
};

这篇关于ng-repeat中的Angular JS绑定范围数据以形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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