詳解基于Bootstrap+angular的一個豆瓣電影app
1、搭建項(xiàng)目框架
npm初始化項(xiàng)目
npm init -y //按默認(rèn)配置初始化項(xiàng)目
安裝需要的第三方庫
npm install bootstrap angular angular-route --save
新建一個index.html頁面 引用 這三個依賴庫
新建兩個文件夾coming_soon in_theaters
然后在這兩個文件夾里分別創(chuàng)建一個controller.js 文件和view.html文件
最后項(xiàng)目文件的結(jié)構(gòu)是這樣
2、搭建首頁樣式
采用bootstrap
http://v3.bootcss.com/examples/dashboard/
該頁面的樣式
然后還需要引用這一個css文件
http://v3.bootcss.com/examples/dashboard/dashboard.css
然后刪掉一些不需要的標(biāo)簽
最后形成的界面
到這邊后,項(xiàng)目的基本結(jié)構(gòu)與樣式搭建完成
3、配置angular路由
到in_theaters下的controller.js文件中 寫上
(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標(biāo)簽加上指令
<body ng-app="movie">
在內(nèi)容顯示模塊中加上ng-view指令
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main" ng-view> </div>
引用app.js
<script src="/js/app.js"></script>
最后瀏覽index.html
說明一切配置正常
4、豆瓣API
豆瓣API開發(fā)者文檔
https://developers.douban.com/wiki/?title=movie_v2
這邊采用jsonp方式獲取數(shù)據(jù)、
由于angular的jsonp方式豆瓣不支持、所以這邊自己封裝了一個jsonp組件
新建一個components文件夾、在該文件夾下創(chuàng)建http.js文件 寫上
(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請求數(shù)據(jù)封裝到$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);
然后在對應(yīng)的view.html中修改成
<h1 class="page-header">{{data.title}}</h1> <div class="list-group"> <a href="{{item.alt}}" rel="external nofollow" rel="external nofollow" 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>導(dǎo)演:<span ng-repeat="d in item.casts">{{d.name +($last?'':'、')}}</span></p> </div> </div> </a> </div>
對應(yīng)的也在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);
對應(yīng)的view.html 修改成
<h1 class="page-header">{{data.title}}</h1> <div class="list-group"> <a href="{{item.alt}}" rel="external nofollow" rel="external nofollow" 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>導(dǎo)演:<span ng-repeat="d in item.casts">{{d.name +($last?'':'、')}}</span></p> </div> </div> </a> </div>
最后別忘了在index.html最后引用
<script src="/components/http.js"></script>
最后展示的效果為
項(xiàng)目到這邊已經(jīng)完成的差不多了
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Angular.js與Bootstrap相結(jié)合實(shí)現(xiàn)表格分頁代碼
- angularJS與bootstrap結(jié)合實(shí)現(xiàn)動態(tài)加載彈出提示內(nèi)容
- BootStrap+Angularjs+NgDialog實(shí)現(xiàn)模式對話框
- Angular2中Bootstrap界面庫ng-bootstrap詳解
- 自定義Angular指令與jQuery實(shí)現(xiàn)的Bootstrap風(fēng)格數(shù)據(jù)雙向綁定的單選與多選下拉框
- 利用Angularjs和bootstrap實(shí)現(xiàn)購物車功能
- Bootstrap和Angularjs配合自制彈框的實(shí)例代碼
- AngularJs中Bootstrap3 datetimepicker使用實(shí)例
- AngularJS 與Bootstrap實(shí)現(xiàn)表格分頁實(shí)例代碼
- AngularJS Bootstrap詳細(xì)介紹及實(shí)例代碼
相關(guān)文章
在 Angular6 中使用 HTTP 請求服務(wù)端數(shù)據(jù)的步驟詳解
本文分步驟給大家介紹了在 Angular6 中使用 HTTP 請求服務(wù)端數(shù)據(jù)的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08Angular 13+開發(fā)模式慢的原因及構(gòu)建性能優(yōu)化解析
這篇文章主要為大家介紹了Angular 13+開發(fā)模式慢的原因及構(gòu)建性能優(yōu)化解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12在 Angular2 中實(shí)現(xiàn)自定義校驗(yàn)指令(確認(rèn)密碼)的方法
這篇文章給大家探索 Angular 2 內(nèi)建的自定義驗(yàn)證,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01淺談angularjs $http提交數(shù)據(jù)探索
這篇文章主要介紹了淺談angularjs $http提交數(shù)據(jù)探索,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-01-01深入理解Angularjs中的$resource服務(wù)
大家可以知道在Angularjs中可以用$http同服務(wù)器進(jìn)行通信,功能上比較簡單,AngularJS還提供了另外一個可選的服務(wù)$resource,使用它可以非常方便的同支持restful的服務(wù)單進(jìn)行數(shù)據(jù)交互。這篇文章主要給大家深入的介紹了Angularjs中的$resource服務(wù)。2016-12-12angular+bootstrap的雙向數(shù)據(jù)綁定實(shí)例
本篇文章主要介紹angular+bootstrap的雙向數(shù)據(jù)綁定的實(shí)例,具有很好的參考價值。下面跟著小編一起來看下吧2017-03-03Angular2學(xué)習(xí)教程之TemplateRef和ViewContainerRef詳解
這篇文章主要給大家介紹了Angular2中TemplateRef和ViewContainerRef的相關(guān)資料,文中介紹的非常詳細(xì),對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-05-05