New to AngularJSs. Wondering why setTimeout
is not working. Is it true that it doe snot work with AngularJS?
jsfiddle.net
<div ng-controller="MyCtrl">
<select ng-model='form' ng-options='option.value as option.name for option in typeOptions'></select>
</div>
<script>
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
//$scope.typeOptions = [];
alert("hi23");
$timeout(function() {
alert("hi");
$scope.typeOptions =
[
{ name: 'Feature', value: 'feature' },
{ name: 'Bug', value: 'bug' },
{ name: 'Enhancement', value: 'enhancement' }
];
$scope.form = $scope.typeOptions[1].value;
}, 3000);
}
</script>
Thanks.
you need to inject $timeout
. Observe the following change
function MyCtrl($scope, $timeout) {
....
}
See the $timeout
docs for more information
Furthermore, this style of declaring controllers is not recommended. I would encourage re-fractoring to the following...
myApp.controller('MyCtrl', ['$scope', '$timeout', function($scope, $timeout) {
....
}]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With