Arrayに追加されたオブジェクトとして更新されないNGOPTIONS
-
21-12-2019 - |
質問
オブジェクトを含む配列を持っています。このリストに新しいオブジェクトをプッシュすると、ビューは新しいオブジェクトを追加するために更新されません。$ scopeと関係があると思います。$が適用されますが、それを使う方法はわかりません。私はこれを中心にプッシュ機能を包むことを試みましたが、工場は$ 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
は約束を返し、その約束を$scope.site.lists
に追加するオブジェクトと解決します。ただし、コントローラにこの大きなロジックを持たないでください。これをさらに説明し、これらの詳細を隠す単一の方法を持つことによってさらに。あなたのコントローラーは単に:
someService.someMethod().then(function(result) {
$scope.whatever.push(result);
});
. 所属していません StackOverflow