简体   繁体   English

在AngularJS中单击行时如何显示模式中特定行的详细信息

[英]How to show the details of a particular row in a modal when row is clicked in AngularJS

I'm new to AngularJS. 我是AngularJS的新手。 I'm converting some pages to AngularJS. 我正在将某些页面转换为AngularJS。 I just displayed rows of information. 我只显示了信息行。 However I'm having trouble converting the button onclick part to AngularJS. 但是我在将按钮onclick部件转换为AngularJS时遇到了麻烦。 Can someone please help me. 有人可以帮帮我吗。 Below is the code that I'm working with. 下面是我正在使用的代码。

<div ng-repeat="i in data">
 <p>{{i.name}}</p>
<button class="btn btn-xs btn-default" data-toggle="modal" data-target=".show-ticket-details-modal" onclick="show_details(10)">
          <i class="fa fa-info"></i>
        </button>
</div>

Just pass the model to show_details method 只需将模型传递给show_details方法

show_details(i)

Regards, 问候,

Try using ng-click ( https://docs.angularjs.org/api/ng/directive/ngClick ). 尝试使用ng-clickhttps://docs.angularjs.org/api/ng/directive/ngClick )。 This runs a function in your angular controller when you click an element. 单击元素时,这将在角度控制器中运行一个功能。 All you've got to do is add the ng-click directive to your element, and then build a function with the same name in your angular controller to handle the data. 您要做的就是将ng-click指令添加到您的元素中,然后在您的角度控制器中构建一个具有相同名称的函数来处理数据。

IE, replace onclick="show_details(10)" with: ng-click="show_details(10)" . IE,将onclick="show_details(10)"替换为: ng-click="show_details(10)"

Then, in your controller, build a function with the same name that will handle the data show_details(10) , like: 然后,在您的控制器中,构建一个具有相同名称的函数来处理数据show_details(10) ,例如:

$scope.show_details = function(index) { 
    console.log(index); // will log 10 in the example above
    // do stuff with your index here, 
    // pass data to your angular factory, etc.
};

Note: For <form> elements, you can use the ng-submit directive instead ( https://docs.angularjs.org/api/ng/directive/ngSubmit ). 注意:对于<form>元素,可以改用ng-submit指令( https://docs.angularjs.org/api/ng/directive/ngSubmit )。 Just use ng-submit="someFunction()" instead of ng-click . 只需使用ng-submit="someFunction()"代替ng-click

Another Idea: 另一个想法:

Instead of passing in the number 10 , you could also use track by $index ( https://docs.angularjs.org/api/ng/directive/ngRepeat# ), for example, in your ng-repeat , you could: 除了传递数字10 ,您还可以使用track by $indexhttps://docs.angularjs.org/api/ng/directive/ngRepeat#track by $index ,例如,在ng-repeat ,您可以:

ng-repeat="i in data track by $index"

Now, you can actually just pass $index into your ng-click function, instead of the number 10 : 现在,您实际上可以将$index传递给ng-click函数,而不是数字10

ng-click="show_details($index)" // $index will be 10, if the index of `i` was 10 in `data`

Hope this helps somewhat, let me know if you have any questions! 希望这会有所帮助,如果您有任何疑问,请告诉我! The links included show more examples of their usage! 包含的链接显示了其用法的更多示例!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM