0

I am trying to embed AngularUI modal to one of my web page.

This function fetches data using HTTP get request.

$scope.fetchTimings=function(){
$http({
    method: 'GET',
    url: 'get/xyz'
})
    .success(function(data) {
            return data;
    })
    .error(function(data, status) {
        return {};
    });

};

This is the variable that I am passing to modal:

$scope.newCategory={
  title:'xyz',
  openHours: $scope.fetchTimings()
}

Controller:

var modalInstance = $modal.open({
templateUrl: "addcategory.html",
controller: "addCategoryModalInstanceCtrl",
resolve: {
    newCategory: function() {
        return $scope.newCategory;
    }
}
});

Modal Instal Controller:

.controller('addCategoryModalInstanceCtrl', function($scope, $modalInstance, newCategory) {
    $scope.newCategory = newCategory;
    $scope.addedNewCategory = $scope.newCategory;
    $scope.ok = function() {
        $modalInstance.close($scope.addedNewCategory);
    };

    $scope.cancel = function() {
        $modalInstance.dismiss('cancel');
    };
})

View: In view, {{newCategory.title}} is being rendered, while {{newCategory.openHours}} shows empty objects.

I guess, this is related to some async delay. How can I fix this problem?

-Thanks

1
  • fetchTimings always returns nothing, i mean there is no return statement -) Commented Dec 10, 2014 at 18:55

1 Answer 1

2

That's exactly what resolve is meant for, it wil keep your controller waiting untill everything in there is resolved:

Controller:

var modalInstance = $modal.open({
    templateUrl: "addcategory.html",
    controller: "addCategoryModalInstanceCtrl",
    resolve: {
        newCategory: function() {
            return $scope.newCategory;
        },
        timings: ['$http', function($http){
            return $http({
                method: 'GET',
                url: '/someUrl'
            });
        }]
    }
});

Modal instance controller:

.controller('addCategoryModalInstanceCtrl',
    function($scope, $modalInstance, newCategory, timings) {
        $scope.timings = timings;
        $scope.newCategory = newCategory;
        $scope.addedNewCategory = $scope.newCategory;
        $scope.ok = function() {
            $modalInstance.close($scope.addedNewCategory);
        };

        $scope.cancel = function() {
            $modalInstance.dismiss('cancel');
        };
    }
)

See the reference for ui-router resolve: https://github.com/angular-ui/ui-router/wiki#resolve

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.