SetTimeout () not working in angularjs?

New to AngularJS. I wonder why it setTimeoutdoesn’t work. Is this a solo job 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>

Thank.

+4
source share
2 answers

you need to enter $timeout. Observe the following change:

function MyCtrl($scope, $timeout) { 
    ....
}

See docs for more details.$timeout


In addition, this style of declaring controllers is not recommended. I would recommend re-splitting into the following ...

myApp.controller('MyCtrl', ['$scope', '$timeout', function($scope, $timeout) {
    .... 
}]);
+6
source

Angular , , . Angular , , DOM .

setTimeout, , setTimeout Angular, Angular.

, $timeout, , setTimeout $scope. $apply, Angular .

JQuery-, Angular. Angular .

+2
source

All Articles