0

オブジェクトを含む配列があります。このリストに新しいオブジェクトをプッシュしても、ビューが更新されず、新しいオブジェクトが追加されません。$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>

コントロール:

    $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")
      }
    });
}
4

1 に答える 1

0

このコードは、すぐにフラグをスローします。

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);
});

この場合、promise をcreateList返し、追加するオブジェクトでその promise を解決する必要があります$scope.site.lists。ただし、コントローラーにはこれほど多くのロジックを含めないでください。プロミスを返し、これらの詳細を非表示にする単一のメソッドを使用して、これをさらに抽象化します。コントローラーには次のものが必要です。

someService.someMethod().then(function(result) {
  $scope.whatever.push(result);
});
于 2014-02-26T01:17:07.753 に答える