我刚开始学习 angular.js。你们能告诉我正确的方法来制作一个页面,该页面最初显示一个 ajax 加载器元素,上面写着“加载数据”或类似的东西。然后在获取数据后,它将更新 View 并隐藏元素。我可以使用 jquery 将内容放入页面加载事件中,但是如何使用纯 Angular 来做到这一点?到目前为止,我想出了如何将其放入点击事件中:
<div ng-app="VideoStatus" ng-controller="VideoStatusCtrl">
<button ng-click="getVideos()">get videos</button>
</div>
<script type="text/javascript">
angular.module('VideoStatus', ['ngResource']).run(function(){
// I guess somehow I can start fetching data from the server here,
// but I don't know how to call Controller methods passing the right scope
});
function VideoStatusCtrl($scope, $resource) {
$scope.videoStatus = $resource('/Videos/GetStatuses', { callback: 'JSON_CALLBACK' });
$scope.getVideos = function () {
$scope.videoResult = $scope.videoStatus.get();
console.log('videos fetched');
};
};
</script>
最佳答案
感谢 Adam Webber & Peter Bacon Darwin
这是工作 plunker
这是我的版本 plunker 使加载成为具有模态弹出功能的指令
这是使用我的版本的教程
您只需要 loading.js 和 modal.js 以及引用 jQuery 和 twitterbootstrap css。
在你的代码中,
只需 2 个步骤即可处理您的代码。
将以下代码添加到HTML
将 LoadingModule 模块添加到您的应用程序模块。
angular.module('YourApp', ['LoadingModule'])
关于javascript - 加载数据ajax风格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13257524/