배열에 객체가 추가되면 ngOptions가 업데이트되지 않습니다.
-
21-12-2019 - |
문제
개체가 포함된 배열이 있습니다.이 목록에 새 개체를 푸시하면 새 개체를 추가하기 위해 보기가 업데이트되지 않습니다.$scope.$apply와 관련이 있다고 가정하지만 사용 방법을 잘 모르겠습니다.이 문제를 중심으로 푸시 기능을 래핑해 보았지만 공장에서는 $scope가 정의되지 않았다고 말합니다.
보다:
<label for="groupOwner">List Template:
<select
id="listTemplate"
ng-model="newList.template"
ng-options="t.name for t in listTemplates|orderBy: 'name'"
></select>
</label>
Ctrl 키:
$scope.createList = function (){
var modalForm = '/Style%20Library/projects/spDash/app/partials/newList.html';
var modalInstance = $modal.open({
templateUrl: modalForm,
backdrop: true,
windowClass: 'modal',
controller: 'newListCtrl',
resolve: {
newListData: function (){
return $scope.newList;
}
}
});
modalInstance.result.then(function(newList){
SiteService.createList(newList,$scope.site);
});
};
서비스 기능:
var createList = function (newList, site){
var promise = $().SPServices({
operation: "AddList",
webURL: site.url,
listName: newList.name,
description: newList.description,
templateID: newList.template.id
})
promise.then(function (){
addToQuickLaunch(newList.name,site.url)
getSiteInfo(site);
//take new list object and push to siteLists array
siteLists.push(newList);
},function (reason){
console.log('Failed: ' + reason);
})
}
function addToQuickLaunch (name,siteUrl) {
$().SPServices({
operation: "UpdateList",
webURL: siteUrl,
listName: name,
listProperties: "<List OnQuickLaunch='TRUE' EnableVersioning='TRUE'/>",
completefunc: function(xData,Status){
console.log(name + " list created")
}
});
}
해결책
이 코드는 나에게 즉각적인 플래그를 던집니다.
modalInstance.result.then(function(newList){
SiteService.createList(newList,$scope.site);
});
그런 서비스를 통해 컨트롤러의 배열을 전달하면 안 됩니다.대신 다음을 수행하십시오.
modalInstance.result.then(function(newList){
return SiteService.createList(newList);
}).then(function(list) {
$scope.site.lists.push(list);
});
이 경우에는 createList
Promise를 반환하고 추가할 객체로 해당 Promise를 해결합니다. $scope.site.lists
.그러나 컨트롤러에 이렇게 많은 로직이 있어서는 안 된다는 점에 유의해야 합니다.Promise를 반환하고 이러한 세부 정보를 숨기는 단일 메서드를 사용하여 이를 더욱 추상화합니다.컨트롤러에는 다음이 있어야 합니다.
someService.someMethod().then(function(result) {
$scope.whatever.push(result);
});
제휴하지 않습니다 StackOverflow