从外部模态的所有API调用完成后更新内部模态

时间:2018-01-05 15:38:02

标签: angularjs

由于内部循环的异步API调用,我面临问题。

我有两个模态打开(一个模态高于其他模态)当我点击顶部模态中的按钮然后它在循环中调用API并且在完成所有调用之后我关闭顶部模态但底部模态值应该反映。

底部模态中的行反映两次,因为我的广播处于循环中。 如果我把广播放在循环之外,那么什么都不会更新。

例如(在顶部模态的按钮上单击):

app.controller('fileSaveSectionCtrl', function ($scope, $rootScope, $modalInstance) {
$scope.uploadFiles = function () {
   for(var i=0; i< selectedFiles.length;i++){
     uploadFiles.saveFile(payload).success(function(response) {
                // API call success
                $modalInstance.close();
                // this calls broadcast 2 times giving single row twice
                $rootScope.$broadcast('fileSaveUpdate');        
     }).error(function(data, status) {
                // some error                   
    });
   }       
 }
}

底部模态控制器:

app.controller('fileUpdateSectionCtrl', function ($scope, $rootScope) {
    /**
     * File Save view on file save should update
     * 
     */
    $scope.$on('fileSaveUpdate', function() {       
        updateFileSaveData();   
        $scope.filesGridOptions.data = filesArray;
        $scope.data = $scope.filesGridOptions.data;
    });

2 个答案:

答案 0 :(得分:1)

这可以使用承诺完成,如下所示:

01-05 13:39:27.142 7233-7240/? I/art: Starting a blocking GC Instrumentation
01-05 13:39:27.156 7233-7233/? W/ActivityThread: Application com.protoreactnative is waiting for the debugger on port 8100...
01-05 13:39:27.156 7233-7233/? I/System.out: Sending WAIT chunk
01-05 13:39:29.228 7233-7240/com.protoreactnative I/art: Debugger is active
01-05 13:39:29.365 7233-7233/com.protoreactnative I/System.out: Debugger has connected
01-05 13:39:29.365 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:29.567 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:29.768 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:29.970 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:30.171 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:30.372 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:30.573 7233-7233/com.protoreactnative I/System.out: waiting for debugger to settle...
01-05 13:39:30.775 7233-7233/com.protoreactnative I/System.out: debugger has settled (1451)
01-05 13:39:31.131 7233-7233/com.protoreactnative I/InstantRun: starting instant run server: is main process
01-05 13:39:31.142 7233-7233/com.protoreactnative V/fb-UnpackingSoSource: locked dso store /data/user/0/com.protoreactnative/lib-main
01-05 13:39:31.143 7233-7233/com.protoreactnative I/fb-UnpackingSoSource: dso store is up-to-date: /data/user/0/com.protoreactnative/lib-main
01-05 13:39:31.143 7233-7233/com.protoreactnative V/fb-UnpackingSoSource: releasing dso store lock for /data/user/0/com.protoreactnative/lib-main
01-05 13:39:31.217 7233-7240/com.protoreactnative I/art: Starting a blocking GC Instrumentation
01-05 13:39:31.222 7233-7233/com.protoreactnative D/ReactNative: ReactInstanceManager.ctor()
01-05 13:39:31.272 7233-7233/com.protoreactnative D/NetworkSecurityConfig: No Network Security Config specified, using platform default
01-05 13:39:31.292 7233-7233/com.protoreactnative I/SensorManager: registerListenerImpl: listener = com.facebook.react.common.ShakeDetector@f6f1ff2, sensor = {Sensor name="Accelerometer Sensor", vendor="hTC Corp.", version=1, type=1, maxRange=39.2266, resolution=0.01, power=0.17, minDelay=10000}, delay = 66667, handler = null
01-05 13:39:31.315 7233-7233/com.protoreactnative D/ReactNative: ReactInstanceManager.createReactContextInBackground()
01-05 13:39:31.316 7233-7233/com.protoreactnative D/ReactNative: ReactInstanceManager.recreateReactContextInBackgroundInner()
01-05 13:39:31.318 7233-7233/com.protoreactnative D/ReactNative: ReactInstanceManager.onJSBundleLoadedFromServer()
01-05 13:39:31.321 7233-7233/com.protoreactnative D/ReactNative: ReactInstanceManager.recreateReactContextInBackground()
01-05 13:39:31.321 7233-7233/com.protoreactnative D/ReactNative: ReactInstanceManager.runCreateReactContextOnNewThread()
01-05 13:39:31.359 7233-7254/com.protoreactnative I/art: Thread[15,tid=7254,Native,Thread*=0xf2e86b00,peer=0x12dcf280,"Thread-4"] recursive attempt to load library "/data/app/com.protoreactnative-2/lib/arm/libfb.so"
01-05 13:39:31.364 7233-7254/com.protoreactnative D/ReactNative: ReactInstanceManager.createReactContext()
01-05 13:39:31.394 7233-7233/com.protoreactnative W/unknown:ReactNative: Packager connection already open, nooping.
01-05 13:39:31.396 7233-7258/com.protoreactnative D/libc: [NET] android_getaddrinfofornetcontext+,hn 9(0x6c6f63616c686f),sn(),hints(known),family 0,flags 1024, proc=com.protoreactnative
01-05 13:39:31.396 7233-7255/com.protoreactnative D/libc: [NET] android_getaddrinfofornetcontext+,hn 9(0x6c6f63616c686f),sn(),hints(known),family 0,flags 1024, proc=com.protoreactnative
01-05 13:39:31.396 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfofornetcontext+,hn 9(0x6c6f63616c686f),sn(),hints(known),family 0,flags 1024, proc=com.protoreactnative
01-05 13:39:31.397 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy get netid:0
01-05 13:39:31.397 7233-7255/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy get netid:0
01-05 13:39:31.399 7233-7255/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy-, success
01-05 13:39:31.401 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy-, success
01-05 13:39:31.402 7233-7258/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy get netid:0
01-05 13:39:31.403 7233-7258/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy-, success
01-05 13:39:31.411 7233-7257/com.protoreactnative W/unknown:InspectorPackagerConnection: Couldn't connect to packager, will silently retry
01-05 13:39:31.457 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTGroupViewManager
01-05 13:39:31.467 7233-7262/com.protoreactnative I/Adreno: QUALCOMM build                   : b756f35, I9267f384df
                                                            Build Date                       : 10/19/16
                                                            OpenGL ES Shader Compiler Version: XE031.09.00.03
                                                            Local Branch                     : 
                                                            Remote Branch                    : refs/tags/AU_LINUX_ANDROID_LA.BF64.1.2.3_RB1.07.00.00.258.005
                                                            Remote Branch                    : NONE
                                                            Reconstruct Branch               : NOTHING
01-05 13:39:31.467 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTGroupShadowNode
01-05 13:39:31.489 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTShapeViewManager
01-05 13:39:31.490 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTShapeShadowNode
01-05 13:39:31.503 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTTextViewManager
01-05 13:39:31.504 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTTextShadowNode
01-05 13:39:31.509 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.checkbox.ReactCheckBoxManager
01-05 13:39:31.540 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.uimanager.LayoutShadowNode
01-05 13:39:31.589 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.picker.ReactDialogPickerManager
01-05 13:39:31.600 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.drawer.ReactDrawerLayoutManager
01-05 13:39:31.608 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.picker.ReactDropdownPickerManager
01-05 13:39:31.612 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.scroll.ReactHorizontalScrollViewManager
01-05 13:39:31.631 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.scroll.ReactHorizontalScrollContainerViewManager
01-05 13:39:31.635 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.progressbar.ReactProgressBarViewManager
01-05 13:39:31.641 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.progressbar.ProgressBarShadowNode
01-05 13:39:31.646 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.scroll.ReactScrollViewManager
01-05 13:39:31.659 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.slider.ReactSliderManager
01-05 13:39:31.665 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.slider.ReactSliderManager$ReactSliderShadowNode
01-05 13:39:31.669 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.switchview.ReactSwitchManager
01-05 13:39:31.672 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.switchview.ReactSwitchManager$ReactSwitchShadowNode
01-05 13:39:31.676 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.toolbar.ReactToolbarManager
01-05 13:39:31.683 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.webview.ReactWebViewManager
01-05 13:39:31.692 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.swiperefresh.SwipeRefreshLayoutManager
01-05 13:39:31.697 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTSurfaceViewManager
01-05 13:39:31.699 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.art.ARTSurfaceViewShadowNode
01-05 13:39:31.704 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.frescosupport.FrescoBasedReactTextInlineImageViewManager
01-05 13:39:31.705 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.frescosupport.FrescoBasedReactTextInlineImageShadowNode
01-05 13:39:31.711 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.image.ReactImageManager
01-05 13:39:31.720 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.modal.ReactModalHostManager
01-05 13:39:31.723 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.modal.ModalHostShadowNode
01-05 13:39:31.725 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.ReactRawTextManager
01-05 13:39:31.726 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.ReactRawTextShadowNode
01-05 13:39:31.728 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.textinput.ReactTextInputManager
01-05 13:39:31.746 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.textinput.ReactTextInputShadowNode
01-05 13:39:31.757 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.ReactTextViewManager
01-05 13:39:31.764 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.ReactTextShadowNode
01-05 13:39:31.768 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.view.ReactViewManager
01-05 13:39:31.778 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.viewpager.ReactViewPagerManager
01-05 13:39:31.782 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.ReactVirtualTextViewManager
01-05 13:39:31.783 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.facebook.react.views.text.ReactVirtualTextShadowNode
01-05 13:39:31.785 7233-7254/com.protoreactnative W/unknown:ViewManagerPropertyUpdater: Could not find generated setter for class com.protoreactnative.react.React3DViewManager
01-05 13:39:31.844 7233-7254/com.protoreactnative D/ReactNative: Initializing React Xplat Bridge.
01-05 13:39:31.849 7233-7254/com.protoreactnative D/ReactNative: Initializing React Xplat Bridge before initializeBridge
01-05 13:39:31.863 7233-7254/com.protoreactnative D/ReactNative: Initializing React Xplat Bridge after initializeBridge
01-05 13:39:31.863 7233-7254/com.protoreactnative D/ReactNative: CatalystInstanceImpl.runJSBundle()
01-05 13:39:31.864 7233-7270/com.protoreactnative D/ReactNative: ReactInstanceManager.setupReactContext()
01-05 13:39:31.864 7233-7270/com.protoreactnative D/ReactNative: CatalystInstanceImpl.initialize()
01-05 13:39:31.866 7233-7270/com.protoreactnative W/unknown:ReactNative: Packager connection already open, nooping.
01-05 13:39:31.871 7233-7270/com.protoreactnative D/ReactNative: ReactInstanceManager.attachRootViewToInstance()
01-05 13:39:32.249 7233-7270/com.protoreactnative I/WebViewFactory: Loading com.android.chrome version 62.0.3202.84 (code 320208452)
01-05 13:39:32.293 7233-7270/com.protoreactnative I/cr_LibraryLoader: Time to load native libraries: 6 ms (timestamps 2695-2701)
01-05 13:39:32.311 7233-7270/com.protoreactnative I/chromium: [INFO:library_loader_hooks.cc(46)] Chromium logging enabled: level = 0, default verbosity = 0
01-05 13:39:32.313 7233-7270/com.protoreactnative I/cr_LibraryLoader: Expected native library version number "62.0.3202.84", actual native library version number "62.0.3202.84"
01-05 13:39:32.467 7233-7237/com.protoreactnative I/art: Do partial code cache collection, code=31KB, data=30KB
01-05 13:39:32.467 7233-7237/com.protoreactnative I/art: After code cache collection, code=30KB, data=29KB
01-05 13:39:32.467 7233-7237/com.protoreactnative I/art: Increasing code cache capacity to 128KB
01-05 13:39:32.692 7233-7269/com.protoreactnative I/ReactNativeJS: Running application "protoreactnative" with appParams: {"rootTag":1}. __DEV__ === true, development-level warning are ON, performance optimizations are OFF
01-05 13:40:29.776 7233-7233/com.protoreactnative I/Unity: onResume
01-05 13:40:29.797 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfofornetcontext+,hn 9(0x6c6f63616c686f),sn(),hints(known),family 0,flags 1024, proc=com.protoreactnative
01-05 13:40:29.797 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy get netid:0
01-05 13:40:29.799 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy-, success
01-05 13:40:29.827 7233-7233/com.protoreactnative D/Unity: SetWindow 0 0xe3b38e08
01-05 13:40:29.827 7233-7233/com.protoreactnative D/Unity: SetWindow 0 0xe3b38e08
01-05 13:40:31.871 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfofornetcontext+,hn 9(0x6c6f63616c686f),sn(),hints(known),family 0,flags 1024, proc=com.protoreactnative
01-05 13:40:31.872 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy get netid:0
01-05 13:40:31.881 7233-7257/com.protoreactnative D/libc: [NET] android_getaddrinfo_proxy-, success

答案 1 :(得分:0)

使用$q.all()一次性发送所有请求,然后在解决所有响应后触发事件。

app.controller('fileSaveSectionCtrl', function($q, $scope, $rootScope, $modalInstance) {
            $scope.uploadFiles = function() {
                var promises = [];
                for (var i = 0; i < selectedFiles.length; i++) {
                    promises.push(uploadFiles.saveFile(payload))
                }
                $q.all(promises).then(function() {
                    $modalInstance.close();
                    $rootScope.$broadcast('fileSaveUpdate');
                });
                }
            }