검색 목록이 있습니다.AngularJs에서 방금 생성 된 객체를 제거하십시오.
search.html에 :
<ul class="col-xs-12" ng-repeat="search in searchesCtrl.searches">
<li>
<a href="#">{{search.url}}</a><button class="btn btn-danger" ng-click="searchesCtrl.deleteSearch(search)">Delete</button>
</li>
</ul>
<div class="input-group">
<input type="text" class="form-control" ng-model="searchesCtrl.newSearch.name"/>
<input type="text" class="form-control" ng-model="searchesCtrl.newSearch.url"/>
<span class="input-group-btn">
<a class="btn btn-primary" ng-click="searchesCtrl.addNewSearch()">Go</a>
</span>
</div>
search.controller.js :
'use strict';
(function() {
class SearchesComponent {
constructor($http) {
this.$http = $http;
this.searches = [];
}
$onInit() {
this.$http.get('/api/searches')
.then(response => {
this.searches = response.data;
});
}
addNewSearch() {
if (this.newSearch) {
this.$http.post('/api/searches', {
url: this.newSearch.url,
name: this.newSearch.name
}).then(() => {
this.searches.push(this.newSearch);
this.newSearch = '';
});
}
}
deleteSearch(search) {
this.$http.delete('/api/searches/' + search._id)
.then(() => {
this.searches.splice(this.searches.indexOf(search),1);
});
}
}
angular.module('myApp')
.component('searches', {
templateUrl: 'app/searches/searches.html',
controller: SearchesComponent,
controllerAs: 'searchesCtrl'
});
})();
내가 페이지를 새로 고치지 않고, 내가 방금 추가 한 검색을 제거하려고하면, 작동하지 않습니다.
ng-click="searchesCtrl.deleteSearch(search)"
은 (는) /api/searches/undefined
입니다.
나는 $ 인덱스 솔루션없이 작업하려고합니다. 가능한가?
당신의'function'의 어느 부분에서 당신은'undefined'지고있다? – developer033
개체 매개 변수 "search._id"는 방금 생성 되었기 때문에 정의되지 않았습니다. –
서버가'$ http.post ('/ api/searches'..)에 새롭게 생성 된 객체를 반환합니까? 그러면 (newSearch => ...'? 서버에서'search._id'를 어떻게 가져 가야합니까? –