简体   繁体   English

仅在angularjs手风琴打开时如何调用函数

[英]How to call a function when angularjs accordion open only

Hi I am using angularjs ui bootstrap accordion. 嗨,我正在使用angularjs ui bootstrap手风琴。 Here i want to call when user click on accordion call one function and get user 当用户单击手风琴时,我想在此调用一个功能并获取用户

Here is the code please help me thnks in advance 这是代码,请提前帮助我

 $scope.load_userdata= function(user_id) { console.log(user_id); } 
 <uib-accordion close-others="oneAtATime"> <div uib-accordion-group class="panel-default" ng-repeat="user in users"> <uib-accordion-heading ng-click="load_userdata(user.id)"> <div class="table"> <div style="width: 50%"> @{{user.name}} </div> <div> Load user data </div> </div> </uib-accordion-heading> </div> </uib-accordion> 

data and display it 数据并显示

I don't really understand what the problem is. 我真的不明白问题是什么。 I copied your template in the snippet below and added a simple controller with a working example. 我在下面的代码段中复制了您的模板,并添加了带有工作示例的简单控制器。

The angular-bootstrap module is not included so you don't get the proper rendering. 不包含angular-bootstrap模块,因此您无法获得正确的渲染。 But it shows you how to define the function in your controller for your template to work. 但是它向您展示了如何在控制器中定义函数以使模板起作用。

 function ctrl($scope) { // dummy users data for the snippet purpose $scope.users = [{ name: 'Joe', id: '1', data : 'Married to Jane', }, { name: 'John', id: '2', data : 'Has 3 children', }, { name: 'Jack', id: '3', data : 'Owns a boat', }, { name: 'Bob', id: '4', data : 'Speaks swedish', } ]; $scope.load_userdata = function(user_id) { console.log("Loading user#%s data", user_id); // looking for the right user : let user = $scope.users.find(x => x.id === user_id); // and toggling the dataLoaded property in the user object user.dataLoaded = true; } } 
 <!DOCTYPE html> <html ng-app> <head> <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.1.5/angular.min.js"></script> <meta charset=utf-8 /> <title>Angular JS Demo</title> </head> <body ng-controller="ctrl"> <uib-accordion close-others="oneAtATime"> <div uib-accordion-group class="panel-default" ng-repeat="user in users"> <uib-accordion-heading ng-click="load_userdata(user.id)"> <div class="table"> <div style="width: 50%"> @{{user.name}} </div> <div> Load user data </div> </div> </uib-accordion-heading> <div ng-show="user.dataLoaded"> User's details : {{user.data}} </div> </div> </uib-accordion> </body> </html> 

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

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