Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8714629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:38:40+00:00 2026-06-13T05:38:40+00:00

I am trying to bind a promise to a view. I don’t know if

  • 0

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: the source is a little contrived with the timeout and uses static data, but that’s to make the code easier to diagnose.

EDIT: JSFiddle Page: http://jsfiddle.net/YQwaf/27/

EDIT: SOLUTION: It turned out you can directly bind promises. I had two problems with my original code:

  1. Using setTimeout() instead of angular’s $timeout was a problem. Angular doesn’t know it needs to refresh the UI when the timeout is triggered ( You could solve this with $scope.$apply inside setTimeout, or you can just use $timeout )
  2. Binding to a function that returned a promise was a problem. If it gets called a second time, it makes yet another promise. Better is to set a scope variable to the promise and only create a new promise as needed. (In my case, this was calling $scope.$watch on the Country Code)

HTML:

<div ng:controller="addressValidationController">
    Region Code <select ng:model="regionCode" ng:options="r.code as r.name for r in getRegions()"/>
    Country Code<select ng:model="countryCode"><option value="US">United States</option><option value="CA">Canada</option></select>
</div>

JS:

function addressValidationController($scope, $q) {
    var regions = {
        US: [{code: 'WI',name: 'Wisconsin'}, {code: 'MN',name: 'Minnesota'}], 
        CA: [{code: 'ON',name: 'Ontario'}]
    };
    $scope.getRegions = function () {
        var deferred = $q.defer();
        setTimeout(function () {
            var countryRegions = regions[$scope.countryCode];
            console.log(countryRegions);
            if(countryRegions === undefined) {
                deferred.resolve([]);
            } else {
                deferred.resolve(countryRegions);
            }
        }, 1000);
        return deferred.promise;
    };
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:38:41+00:00Added an answer on June 13, 2026 at 5:38 am

    WARNING: this answer was accurate when it was written, but as of 1.2 the Angular template engine does not handle promises transparently! — @Malvolio

    Yes the template engine (and expressions) handle promises transparently, but I would assign the promise to a scope property in the controller and not call everytime a function that returns a new promise (I think it’s your problem, resolved promise is lost because a new promise is returned everytime).

    JSFiddle: http://jsfiddle.net/YQwaf/36/

    HTML:

    <div ng:controller="addressValidationController">
        Region Code <select ng:model="regionCode" ng:options="r.code as r.name for r in regions"/>
        Country Code<select ng:model="countryCode"><option value="US">United States</option><option value="CA">Canada</option></select>
    </div>
    

    JS:

    function addressValidationController($scope, $q, $timeout) {
        var regions = {
            US: [{
                code: 'WI',
                name: 'Wisconsin'},
            {
                code: 'MN',
                name: 'Minnesota'}],
            CA: [{
                code: 'ON',
                name: 'Ontario'}]
        };
    
        function getRegions(countryCode) {
            console.log('getRegions: ' + countryCode);
            var deferred = $q.defer();
            $timeout(function() {
                var countryRegions = regions[countryCode];
                if (countryRegions === undefined) {
                    console.log('resolve empty');
                    deferred.resolve([]);
                } else {
                    console.log('resolve');
                    deferred.resolve(countryRegions);
                }
            }, 1000);
            return deferred.promise;
        };
    
        $scope.regions = [];
    
        // Manage country changes:
        $scope.$watch('countryCode', function(countryCode) {
            if (angular.isDefined(countryCode)) {
                $scope.regions = getRegions(countryCode);
            }
            else {
                $scope.regions = [];
            }
        });
    }​
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to bind a list of data to a data grid, but can't
im trying to bind a list-view with a generic list but it seems im
Hi I'm trying to bind data to a kendochart, but no bars are appearing
I'm trying to bind parameters in what I think is the correct way, but
I'm trying to bind a XAML ComboBox so that its list items are the
I'm trying to bind to the ActualWidth of a column but the binding doesn't
I'm trying to bind a 1-many mapping using KnockoutJS, where 1 zip code can
Im trying to bind a list of images to a Listbox, but all I
I'm trying to bind a <ul> element to a property that is an observableArray
I'm trying to bind a ComboBox to an ObservableCollection<MyType> but it's not working as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.