基于AngularJS前端路由,实现一个最简单的图片翻页查看器

<!DOCTYPE html>
<html>
<head>
<title>基于AngularJS前端路由的图片显示控件</title>
<Meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<Meta http-equiv="Expires" content="0">
<Meta http-equiv="Pragma" content="no-cache">
<Meta http-equiv="Cache-control" content="no-cache">
<Meta http-equiv="Cache" content="no-cache">
<style>
body{
width: 800px;
height: 480px;
margin: 0;
padding: 0;
}
#contentview{
width: 100%;
height: 100%;
}
#contentview #current_img{
width: 100%; /* or any custom size */
height: 100%;
max-height: 100%;
max-width: 100%;
object-fit: contain;
object-position: 0 0;
}
</style>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript" src="angular-1.0.8.js"></script>
<!--script type="text/javascript" src="angular.min.js"></script-->
<script>
var imgurls = [ //图片url列表;
    "1.jpg","2.jpg","3.jpg","4.jpg"
];
</script>
</head>
<body style="background: black;" ng-app="myApp" ng-controller="ImageViewerCtrl">
    <div ng-view id="contentview">
        <!-- image viewer controller's content -->
        <!--img id="current_img" src="{{$parent.imgurls[$parent.imgId]}}" ng-cloak /-->
    </div>
    <script type="text/javascript">
        window.onerror = function(e){
            console.log(e);
        }
        var myAppModule = angular.module('myApp',[]);
        myAppModule.config(['$routeProvider',function($routeProvider){
            $routeProvider
                .when('/image/:id',{
                    template: '<img id="current_img" src="{{imgurls[imgId]}}" ng-click="gotoNextimage()" img-autosize0 />',//templateUrl: '/partials/image-viewer.tpl.html',controller: 'ImageViewerCtrl'
                })
                .otherwise({redirectTo: '/image/0'}); //在浏览器强制刷新的情况下,还是会先加载/image/0(虽然没有显示出来)
        }]);
        
        myAppModule.controller('ImageViewerCtrl',function($scope,$location,$timeout,$route,$routeParams){
            $scope.angular = angular; //exposed;
            $scope.imgurls = imgurls;
            console.log("$routeParams="+ JSON.stringify($routeParams));
            $scope.imgId = $routeParams.id || 0; //undefined? why?
            console.log("$scope.imgId="+$scope.imgId);
            
            /*
            $timeout(function() {
                console.log("try to call $location.path");
                $location.path("/image/0");
            },2000);
            */
            $scope.gotoNextimage = function(){
                var nextImgId;
                if(Number($scope.imgId)<$scope.imgurls.length-1)
                    nextImgId = Number($scope.imgId) + 1;
                else
                    nextImgId = 0;
                $location.path("/image/"+nextImgId);
            }
        });
        
        myAppModule.directive("imgAutosize",function(){
            return {
                restrict: 'EA',compile: function(element,attrs) {
                    $(element).on("load",function(){
                        var width = $(element.parentNode).width();
                        var height = $(element.parentNode).height();
                        $(element).css({
                            width: "100%",height: 'auto'
                        })
                    });
                }
            };
        })
    </script>
    <footer>angular.version.full: {{angular.version.full}}</footer>
</body>
</html>


初学者障碍:一开始配置route path的时候,使用了相对路径,而不是绝对路径(/开始),导致指定的controller始终不走,而且没有任何报错,简直活见鬼

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...