NGOPTIONS NON AGGIORNAMENTO AS AGGIUNTI OGGETTI ARRICANTI ALLA ARRAY
-
21-12-2019 - |
Domanda
Ho un array che contiene oggetti.Quando premo un nuovo oggetto su questo elenco, la vista non si aggiorna per aggiungere il nuovo oggetto.Presumo che abbia a che fare con la portata $. $ Applica ma non sono sicuro di come usarlo.Ho provato a avvolgere la funzione di spinta attorno a questo, ma la fabbrica dice che $ ambito non è definito.
Vista:
<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);
});
};
.
Funzione di servizio:
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")
}
});
}
. Soluzione
Questo codice lancia una bandiera immediata a me:
modalInstance.result.then(function(newList){
SiteService.createList(newList,$scope.site);
});
.
Non è necessario passare l'array dal controller attraverso il servizio del genere.Invece, fallo:
modalInstance.result.then(function(newList){
return SiteService.createList(newList);
}).then(function(list) {
$scope.site.lists.push(list);
});
.
In questo caso, avrai createList
restituisce una promessa e risolverai che promette con l'oggetto che deve essere aggiunto a $scope.site.lists
.Tuttavia, dovresti notare che non dovresti avere questa logica molto nel tuo controller.Astratto questo ulteriormente avendo un unico metodo che restituisce una promessa e nascondi questi dettagli.Il tuo controller dovrebbe semplicemente avere:
someService.someMethod().then(function(result) {
$scope.whatever.push(result);
});
.