gpt4 book ai didi

javascript - Angular ionic 切换组

转载 作者:行者123 更新时间:2023-11-30 17:04:54 25 4
gpt4 key购买 nike

我有一组用于使用 angular 和 ionic 编写的 phonegap 应用程序的开关。基本设置是这样的; HTML:

<div ng-controller="LoginCtrl">
<h2 class="title">Items</h2>
<ion-toggle ng-repeat="item in riderList"
ng-model="selectedRider"
ng-checked="item.checked"
ng-change=" updateRider();"
data-rider-id="{{ item.id }}"
>
{{ item.text }}
</ion-toggle>
<button class="button button-full button-positive" id="zpass-logout">
Logout
</button>
</div>

和 js:

angular.module('zpassApp')
.controller('SettingsCtrl', ['$scope', '$http', '$rootScope', '$location',
function($scope, $http, $rootScope, $location) {

$scope.riderList = [];

$scope.addRider = function(name, id, active) {
$scope.riderList.push( { "text" : name, "checked" : active , "id" : id});
$scope.$apply();
};

$scope.updateRider = function() {
console.log("in update rider how do I get the item.id!?" );
// I expect this to be selectedRider.id, but that is undefined
// in jquery would be $(this)..data('id');
}

function getRidersFromDB() {
// this just queries db an populates, works fine...
}

似乎无法获取已更改的切换开关的 ID 和状态。执行此操作的正确方法是什么?

最佳答案

将你的 ng-model 设置为 checked 属性,这样无论是否选中该选项,它都会更新。删除 ng-checked 因为 ng-model 会处理它。为了访问选中的项目,只需在 ng-change 的 updateRider 函数表达式中传递 item 并在那里访问它。

即:

 <ion-toggle ng-repeat="item in riderList"
ng-model="item.checked"
ng-change=" updateRider(item);">
{{ item.text }}
</ion-toggle>

$scope.updateRider = function(item) {
//console.log(item.id, item.checked);
}

Plnkr

关于javascript - Angular ionic 切换组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28205324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com