如何从 ng-repeat 中获取选定的值 [英] How to get selected value from ng-repeat

查看:23
本文介绍了如何从 ng-repeat 中获取选定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.

我正在通过 ng-repeat 获取数据并将其显示为如下代码所示.

I m getting data through ng-repeat and showing it as shown in below code.

我想要的是,如果我点击任何一个名字,它就会用那个名字提醒我.我怎样才能做到这一点?

What I want is if I click on either of the name then it should alert me with that name. How can I achieve this??

var myfriend = angular.module('myfriend',[]);

myfriend.controller('myfriendController', function($scope) 
{
   $scope.record = [
       {     "id" : "01",
            "firstname" : "Mohan ",
            "middlename" : "K",
            "lastname" : "Futterkiste1"
        },{
             "id" : "04",
            "firstname" : "Rohan ",
            "middlename" : "A",
            "lastname" : "Futterkiste2"
        },{
              "id" : "08",
            "firstname" : "sohan ",
            "middlename" : "M",
            "lastname" : "Futterkiste3"
        }
   ]
               
    
});

<html>
  <head>
          <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>

  </head>
  <body ng-app="myfriend">
    
    
    
    <table class="table" style="border:1px red solid; width:100%; "  ng-controller="myfriendController">
		    <thead>
		      <tr>
		      	<th>Id</th>
		        <th>First name</th>
		        <th>Middle name</th>
   		        <th>Last name</th>
		      </tr>
		    </thead>
		    <tbody>
		      <tr ng-repeat="x in record">
   		        <th>{{x.id}}</th>
   		        <th ng-click="selectInfo(x.id)">    {{x.firstname}}</th>
                <th>{{x.middlename}}</th>
                <th>{{x.lastname}}</th>
		      </tr>
		    </tbody>  
	</table> 
  <body>
</html>
 

推荐答案

需要修改html并在controller文件中添加selectInfo函数.

You need to modify html and add selectInfo function in controller file.

html

<table>
      <tr ng-repeat="x in record">
                    <th>{{x.id}}</th>
                    <th ng-click="selectInfo(x.firstname)">    {{x.firstname}}</th>
                    <th ng-click="selectInfo(x.middlename)">{{x.middlename}}</th>
                    <th ng-click="selectInfo(x.lastname)">{{x.lastname}}</th>
                  </tr>
    </table>

代码

$scope.selectInfo=function(name){
alert(name);
}

这篇关于如何从 ng-repeat 中获取选定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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