我刚刚开始使用angularJS
,我正在尝试学习如何在用户点击复选框时在数组中插入字符串,然后再访问这些值。
这是我到目前为止所做的:
<md-button class="toolbar-button" ng-click="removePlan()">
<md-icon>delete</md-icon>
</md-button>
<md-list-item ng-repeat="plan in plans">
<md-checkbox ng-model="plansSelected[plan._id]"> </md-checkbox>
</md-list-item>
apps.js
$scope.plansSelected = []
$scope.removePlan = function () {
for (var plan in $scope.plansSelected) {
if ($scope.plansSelected[plan] === true) {
projectResource.removePlan(plan).then(function (data) {
if (data.success) {
$scope.plans.push(data.plan)
}
})
}
}
}
projectResource.js
resource.removePlan = function (planId) {
return $http.delete(getPlanUrl(planId)).then(processResponse, processError)
}
function getPlansUrl () {
return getProjectUrl() + '/plans'
}
function getPlanUrl () {
return getPlansUrl() + '/' + $route.current.params.planId
}
在调试器中,这就是我的数组的样子:
$scope.plansSelected: Array[0]
57bd40128a58022800887d80:false
57bd40328a58022800887d88:false
57cc2bcbb452140500b0334a:false
我正在检查用户是否从复选框中删除了选项,如果值为true,则取该plan._id并将循环传递给removePlan
函数。
有没有人看到我做错了什么,因为这不起作用。
答案 0 :(得分:1)
请更具体一点&#34;这不起作用&#34;意味着 - 我们很难在没有能够运行它的情况下检测到它。
我可以看到你将$ scope.plansSelected用作空数组是错误的。我会放弃它,并按照这样做:
<md-list-item ng-repeat="plan in plans track by $index">
{{plan.name}} <md-checkbox ng-model="plans[$index].selected"> </md-checkbox>
</md-list-item>
这基本上增加了一个被选中的&#39;属性到您的计划数组,并使您更容易迭代计划数组并拉出您想要的数组。