Reputation: 653
I'm trying to figure out as to why my timeout function is giving error, thereby restricting the change in model value.
angularExample.html
<!DOCTYPE html>
<html ng-app="Tutorial">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
<script type="text/javascript" src="scripts/app.js"></script>
</head>
<body ng-controller="MyController">
<input type="text" ng-model="data" />
</body>
</html>
app.js
(function() {
var app = angular.module('Tutorial', []);
app.controller("MyController",function($scope,$timeout){
$scope.data="hi";
$timeout(callAtTimeout,3000);
var callAtTimeout=function(){$scope.data="hello";}
});
})();
Error Snapshot:
Upvotes: 16
Views: 67934
Reputation: 91
The error can also occur when two or more dependencies are misplaced. I know the answer is kinda off topic but reading this might help someone.
(function() {
'use strict';
angular
.module('app')
.controller('myController', myController);
myController.$inject = ['dependency1','dependency2','dependency3'];
/* @ngInject */
function myController(dependency1, dependency3, dependency2){
// will trigger the error because dependency 2 && 3 are misplaced
var v = dependency2.somefunction(arg1,arg2,...);
// sometimes it can be difficult to see at the first look
// especially when you have many dependencies
}
})();
Upvotes: 1
Reputation: 1167
I had spend a lot of time trying to fix it. It was an internal error in my case. You just have to
break the Angular CLI (Ctrl + c) and
run (ng serve) again.
It then started recognizing all my newly defined functions.
Upvotes: 0
Reputation: 7666
You are defining the callAtTimeout function after it its call. You need to have it above it.
Sample code:
(function () {
var app = angular.module('Tutorial', []);
app.controller("MyController", function ($scope, $timeout) {
var callAtTimeout = function () {
$scope.data = "hello";
}
$scope.data = "hi";
$timeout(callAtTimeout, 3000);
}); })();
Upvotes: 2
Reputation: 32713
You just need to re-arrange the order of your code, the definition for callAtTimeout function should be before you use it. Working example:
(function() {
var app = angular.module('Tutorial', []);
app.controller("MyController",function($scope,$timeout){
var callAtTimeout=function(){$scope.data="hello";}
$scope.data="hi";
$timeout(callAtTimeout,3000);
});
})();
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="Tutorial" ng-controller="MyController">
<input type="text" ng-model="data" />
</body>
Upvotes: 2
Reputation: 7740
Defining functions such as var callAtTimeout = function() { ... }
happens at run time, not at compile time (whereas defining functions such as function callAtTimeout() { ... }
is at compile time).
Because of this, callAtTimeout
is not yet defined on the line:
$timeout(callAtTimeout,3000);
Either move the declaration of callAtTimeout
above that line, or change it to function callAtTimeout() { ... }
Upvotes: 1