Ambuj Jauhari Ambuj Jauhari - 6 months ago 46
Javascript Question

$timeout is not a function angularjs

I am beginner with angular js and I am trying to implement modal with hyperlink, I ham referring to this plunker

http://plnkr.co/edit/M7qfegYIOqOQekoxLaj5?p=preview

I have been able to successfully open modal on hyperlink, but i am not able to display the data returned from function, it always gives error, $timeout is not a function, I am passing an additional parameter as well from ng-click.

<td ng-repeat="column in columns">
<div class="animate-switch" ng-if="column == spaceIdHeaderName">
<a href="" ng-click="open(user[column])"> {{user[column]}} </a></div>
<div class="animate-switch" ng-if="column != spaceIdHeaderName">{{user[column]}}</div>

</td>


My Modal js is as:

$scope.open = function ($timeout, $log, parameter1) {

var modalInstance = $uibModal.open({
templateUrl: 'myModalContent.html',
controller: ModalInstanceCtrl,
resolve: {
items: function ($http) {
return "loadind data...";
}
}
});

modalInstance.opened.then(function($timeout, $log) {
$scope.loadData(modalInstance,$timeout, $log);
}, function() {
$log.info('Modal dismissed at: ' + new Date());
});

$scope.loadData = function(aModalInstance, $timeout, $log) {
$timeout(function() {
aModalInstance.setItems("data loaded...");
}, 3000);
};

};

var ModalInstanceCtrl = function ($scope, $uibModalInstance, items) {
$scope.items = items;

$uibModalInstance.setItems = function(theData) {
$scope.items = theData;
};

$scope.ok = function() {
$uibModalInstance.close('close');
};
$scope.cancel = function() {
$uibModalInstance.dismiss('cancel');
};
};
});


Cant figure out why $timeout is not resolved, is it something related to parameter passing ?

Answer

You need to include $timeout as a dependency in your controller.

Not only that $log and $http also needs to be included.

This statement in your uibModal:

    resolve: {
            items: function ($http) {
                return "loadind data...";
            }
        }

The $scope.items = items will just assign the string "loading data..." to it. I do not know the reason you are passing this to resolve.