Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS:如何等待使用嵌套控制器完成异步API_Javascript_Angularjs - Fatal编程技术网

Javascript AngularJS:如何等待使用嵌套控制器完成异步API

Javascript AngularJS:如何等待使用嵌套控制器完成异步API,javascript,angularjs,Javascript,Angularjs,首先,看看下面的示例代码 // parent-controller.js export default function parentCtrl($scope, MyService) { function init() { MyService.get().$promise.then(data => $scope.data = data); } } // child-controller.js export default function childCtrl

首先,看看下面的示例代码

// parent-controller.js
export default function parentCtrl($scope, MyService) {
    function init() {
        MyService.get().$promise.then(data => $scope.data = data);
    }
}

// child-controller.js
export default function childCtrl($scope) {
    const MY_LIST = ['a', 'b', 'd'];
    function getFoo() {
        if ($scope.data) {
            return MY_LIST.find((el) => el === $scope.data.foo);
        }
    }
    function getBar() {
        if ($scope.data) {
            return MY_LIST.find((el) => el === $scope.data.bar);
        }
    }
}

// my.html
~(snipped)~
<span>{{ getBar() }}</span>
~(snipped)~
<div ng-if="data">
<span>{{ getBar() }}</span>
</div>