pac*_*tie 5 html javascript angularjs
AngularJS新手.想知道为什么setTimeout不工作.它是否真的与SnularJS合作?
<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>
Run Code Online (Sandbox Code Playgroud)
谢谢.
你需要注射$timeout.请注意以下更改
function MyCtrl($scope, $timeout) {
....
}
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅$timeout文档
此外,不建议使用这种声明控制器.我鼓励重新分析以下内容......
myApp.controller('MyCtrl', ['$scope', '$timeout', function($scope, $timeout) {
....
}]);
Run Code Online (Sandbox Code Playgroud)