2、建立首頁樣式採用bootstrap #該頁面的樣式然後還需要引用這一個css檔 然後刪除一些不需要的標籤最後形成的介面
(function(angular){'use strict';var module = angular.module('movie.in_theaters',['ngRoute']); module.config(['$routeProvider',function($routeProvider){ $routeProvider.when('/in_theaters',{ controller: 'inTheatersController', templateUrl: '/in_theaters/view.html'}); }]); module.controller('inTheatersController',['$scope',function($scope){ }]); })(angular);#在view.html寫上
<h1 class="page-header">正在热映</h1>到coming_soon下的controller.js 寫上
(function(angular){'use strict';var module = angular.module('movie.coming_soon',['ngRoute']); module.config(['$routeProvider',function($routeProvider){ $routeProvider.when('/coming_soon',{ controller: 'comingSoonController', templateUrl: '/coming_soon/view.html'}); }]); module.controller('comingSoonController',['$scope',function($scope){ }]); })(angular);#在view .html寫上
<h1 class="page-header">即将上映</h1>然後在js資料夾中新建一個app.js 寫上
(function (angular) {'use strict';var module = angular.module('movie', ['ngRoute', 'movie.in_theaters','movie.coming_soon' ]); module.config(['$routeProvider', function ($routeProvider) { $routeProvider.otherwise({ redirectTo: '/in_theaters'}); }]); })(angular);最後在index.html頁面body標籤加上指令
<body ng-app="movie">#在內容顯示模組中加上ng-view指令
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main" ng-view> </div>
<script src="/js/app.js?1.1.11"></script>
(function (angular) {'use strict';var http = angular.module('movie.http', []); http.service('HttpService', ['$window', '$document', function ($window, $document) {this.jsonp = function (url, data, callback) {var cbFuncName = 'jsonp_fun' +Math.random().toString().replace('.', ''); $window[cbFuncName] = callback;var queryString = url.indexOf('?') == -1 ? '?' : '&';for (var key in data) { queryString += key + '=' + data[key] + '&'; } queryString += 'callback=' + cbFuncName;var script = document.createElement('script'); script.src = url + queryString; $document[0].body.appendChild(script); } }]); })(angular);然後在in_theaters資料夾下的controller.js加入對movie.http模組的依賴,並透過jsonp請求資料封裝到$scope.data中
(function (angular) {'use strict';var module = angular.module('movie.in_theaters', ['ngRoute', 'movie.http']); module.config(['$routeProvider', function ($routeProvider) { $routeProvider.when('/in_theaters', { controller: 'inTheatersController', templateUrl: '/in_theaters/view.html'}); }]); module.controller('inTheatersController', ['$scope', 'HttpService', function ($scope, HttpService) { console.log(HttpService); HttpService.jsonp('http://api.douban.com/v2/movie/in_theaters', { count: 10, start: 0}, function (data) { $scope.data = data; $scope.$apply(); console.log(data); }); }]); })(angular);#然後對應的view.html中修改成
<h1 class="page-header">{{data.title}}</h1><div class="list-group"><a href="{{item.alt}}" class="list-group-item" ng-repeat="item in data.subjects"><span class="badge">{{item.rating.average}}</span><div class="media"><div class="media-left"><img class="media-object" ng-src="{{item.images.small}}" alt=""></div><div class="media-body"><h3 class="media-heading">{{item.title}}</h3><p>类型:<span>{{item.genres.join('、')}}</span></p><p>导演:<span ng-repeat="d in item.casts">{{d.name +($last?'':'、')}}</span></p> </div></div></a></div>對應的也在coming_soon資料夾下的controller.js中修改
(function(angular){'use strict';var module = angular.module('movie.coming_soon',['ngRoute','movie.http']); module.config(['$routeProvider',function($routeProvider){ $routeProvider.when('/coming_soon',{ controller: 'comingSoonController', templateUrl: '/coming_soon/view.html'}); }]); module.controller('comingSoonController',['$scope','HttpService',function($scope,HttpService){ HttpService.jsonp('http://api.douban.com/v2/movie/coming_soon',{ count:10, start:0},function(data){ $scope.data=data; $scope.$apply(); }); }]); })(angular);對應的view .html 修改成
<h1 class="page-header">{{data.title}}</h1><div class="list-group"><a href="{{item.alt}}" class="list-group-item" ng-repeat="item in data.subjects"><span class="badge">{{item.rating.average}}</span><div class="media"><div class="media-left"><img class="media-object" ng-src="{{item.images.small}}" alt=""></div><div class="media-body"><h3 class="media-heading">{{item.title}}</h3><p>类型:<span>{{item.genres.join('、')}}</span></p><p>导演:<span ng-repeat="d in item.casts">{{d.name +($last?'':'、')}}</span></p> </div></div></a></div>最後別忘了在index.html最後引用
<script src="/components/http.js?1.1.11"></script>最後展示的效果為 # 專案到這邊已經完成的差不多了
以上是分享Bootstrap+angular實作豆瓣電影app實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!