2 시간 만에 머리가 부러졌습니다. Angular JS가 처음이기 때문입니다.
다음 확인란이 있고 확인란 값을 배열로 푸시하고 싶습니다. 선택하지 않으면 배열에서 튀어 나와야합니다. 다음 코드를 시도했습니다. 하지만 작동하지 않습니다 ...
<label ng-repeat="r in MessageUserList">
<input type="checkbox" name="selectList[]" value="{{r}}"
ng-checked="selection.indexOf(r) > -1" ng-click="toggleSelection(r)">
{{r.member.first_name}}</label>
웹 서비스에서 오는 MessageUserList JSON은 다음과 같습니다.
{
"member":{
"member_id":8,
"first_name":"Mr. David",
"last_name":"Raz",
"phone":122,
"password":"dd",
"mail":"[email protected]",
"system_date":"May 18, 2017 3:26:01 PM",
"society_id":1,
},
"flat_assign":"N"
}
내 컨트롤러에서 :
var selection=[];
// Toggle selection for a given r by name
$scope.toggleSelection = function toggleSelection(r) {
var idx = $scope.selection.indexOf(r); //Error here indexOf(r)
// Is currently selected
if (idx > -1) {
alert("Same checked");
$scope.selection.splice(idx, 1);
}
// Is newly selected
else {
alert("New checked");
$scope.selection.push(r);
alert(JSON.stringify(selection));
}
}
오류가 발생합니다 : TypeError : b.toggleSelection에서 정의되지 않은 'indexOf'속성을 읽을 수 없습니다 (controllers.js : 129).
이 시도,
<!DOCTYPE html>
<html>
<head>
<script Src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.1/angular.js"></script>
<script type="text/javascript">
var app = angular.module('myApp', []);
app.controller('surveyController', function($scope){
$scope.MessageUserList = [{
"member":{
"member_id":8,
"first_name":"Mr. David",
"last_name":"Raz",
"phone":122,
"password":"dd",
"mail":"[email protected]",
"system_date":"May 18, 2017 3:26:01 PM",
"society_id":1,
},
"flat_assign":"N"
}];
$scope.selection = [];
// Toggle selection for a given r by name
$scope.toggleSelection = function toggleSelection(r) {
var idx = $scope.selection.indexOf(r); //Error here indexOf(r)
// Is currently selected
if (idx > -1) {
alert("Same checked");
$scope.selection.splice(idx, 1);
}
// Is newly selected
else {
alert("New checked");
$scope.selection.push(r);
alert(JSON.stringify($scope.selection));
}
}
});
</script>
</head>
<body ng-controller="surveyController" ng-app="myApp">
<label ng-repeat="r in MessageUserList">
<input type="checkbox" name="selectList[]" value="{{r}}"
ng-checked="selection.indexOf(r) > -1" ng-click="toggleSelection(r)">
{{r.member.first_name}}</label>
</body>
</html>
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다