仅在模型对象上获取过滤数据作为结果,angularjs(Get filtered data only on model object as result, angularjs)

编程入门 行业动态 更新时间:2024-10-26 21:29:04
仅在模型对象上获取过滤数据作为结果,angularjs(Get filtered data only on model object as result, angularjs)

我的代码是这样的

<body ng-app="myApp" ng-controller="MainCtrl"> <div>Name only <input ng-model="search.name" /> <br /> <table id="searchObjResults"> <tr> <th>Name</th> <th>Phone</th> </tr> <tr ng-repeat="friendObj in friends | filter:search:strict | limitTo:1"> <td>{{friendObj.name}}</td> <td>{{friendObj.phone}}</td> </tr> </table> </div> <div> <button type="button" id="btn_submit" ng-click="submitForm()">Get rates</button> </div>

angular.module('myApp', []).controller('MainCtrl', ['$http', '$scope', function ($http, $scope) { $scope.friends = [{ name: 'John', phone: '555-1276' }, { name: 'Mary', phone: '800-BIG-MARY' }, { name: 'Mike', phone: '555-4321' }, { name: 'Adam', phone: '555-5678' }, { name: 'Julie', phone: '555-8765' }, { name: 'Juliette', phone: '555-5678' }]; $scope.submitForm = function () { // i want to get the data here }; }]);

正如您所看到的那样,我的屏幕上只有一位friend会活跃。 当我按下提交按钮时,我希望该数据(已过滤的单行)成为我当前$scope.friends上的唯一值,以便我可以将其作为所选数据发送到外部服务。 任何人都可以在这里指出我需要做的事吗

注意:我无法更改此按钮的位置。

My code is like this

<body ng-app="myApp" ng-controller="MainCtrl"> <div>Name only <input ng-model="search.name" /> <br /> <table id="searchObjResults"> <tr> <th>Name</th> <th>Phone</th> </tr> <tr ng-repeat="friendObj in friends | filter:search:strict | limitTo:1"> <td>{{friendObj.name}}</td> <td>{{friendObj.phone}}</td> </tr> </table> </div> <div> <button type="button" id="btn_submit" ng-click="submitForm()">Get rates</button> </div>

angular.module('myApp', []).controller('MainCtrl', ['$http', '$scope', function ($http, $scope) { $scope.friends = [{ name: 'John', phone: '555-1276' }, { name: 'Mary', phone: '800-BIG-MARY' }, { name: 'Mike', phone: '555-4321' }, { name: 'Adam', phone: '555-5678' }, { name: 'Julie', phone: '555-8765' }, { name: 'Juliette', phone: '555-5678' }]; $scope.submitForm = function () { // i want to get the data here }; }]);

As you can see at a time only one friend will be active on my screen. when I press my submit button, I want that data (filtered single row) to be the only value on my current $scope.friends so that I can send it to an external service as the selected data. Can any one point out what i need to do here Fiddle

Note: I can't change the position of this button.

最满意答案

为什么不让你的按钮成为表格行的一部分,因为只有一个? 这是一个JSFiddle,显示它以这种方式工作 。

然后,按钮的ng-click函数处理程序可以简单地获取您感兴趣的实际friendObj参数:

<button type="button" ng-click="submitForm( friendObj )">Get rates</button>

编辑:如果你不能移动按钮,实际上有一种方法可以做到这一点; 使ng-repeat在新数组上运行,该数组可在ng-repeat之外访问。 所以你的ng-repeat语句变为:

<tr ng-repeat="friendObj in newArray = (friends | filter:search:strict | limitTo:1)">

然后你的按钮可以简单地引用单元素数组:

<button type="button" ng-click="submitForm( newArray )">Get rates</button>

在这里更新了小提琴 :-)

Why not make your button part of the table row, since there will only ever be one? Here is a JSFiddle showing it working in that fashion.

The ng-click function handler for the button can then simply take a parameter that is the actual friendObj you are interested in:

<button type="button" ng-click="submitForm( friendObj )">Get rates</button>

EDIT: There is actually a way to do this if you can't move the button; make the ng-repeat operate over a NEW array, which will be accessible outside of the ng-repeat. So your ng-repeat statement becomes:

<tr ng-repeat="friendObj in newArray = (friends | filter:search:strict | limitTo:1)">

And then your button can simply reference the one-element array:

<button type="button" ng-click="submitForm( newArray )">Get rates</button>

Updated Fiddle here :-)

更多推荐

friends,data,$scope,电脑培训,计算机培训,IT培训"/> <meta name="descripti

本文发布于:2023-07-31 01:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340432.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:模型   对象   数据   angularjs   filtered

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!