Angular JS: how to bind to promises

后端 未结 4 1825
滥情空心
滥情空心 2020-11-28 07:44

I am trying to bind a promise to a view. I don\'t know if you can do that directly, but that\'s what I\'m attempting to do. Any ideas what I am doing wrong?

Note:

4条回答
  •  温柔的废话
    2020-11-28 08:18

    returning a reference to the scope variable holding the list should suffice.

    function addressValidationController($scope,$timeout) {
        var regions = {
            US: [{code: 'WI',name: 'Wisconsin'}, {code: 'MN',name: 'Minnesota'}], 
            CA: [{code: 'ON',name: 'Ontario'}]
        };
    
        $scope._regions = [];
    
        $scope.getRegions = function () {
    
            $timeout(function () {
                var countryRegions = regions[$scope.countryCode];
                console.log(countryRegions);
                if(countryRegions === undefined) {
                    $scope._regions = []
                } else {
                    $scope._regions = countryRegions
                }
            }, 1000);
    
            return $scope._regions;
        };
    }
    

提交回复
热议问题