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>