1. 程式人生 > >ionic爬過的各種坑;(持續更新)

ionic爬過的各種坑;(持續更新)

1:判斷當前裝置是否ios/andriod:

<script type="text/javascript">
var u = navigator.userAgent;
var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android終端
var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios終端
alert('是否是Android:'+isAndroid);
alert('是否是iOS:'+isiOS);
</script>

2:在ion-content中,由於scorll 更新了頁面資料後不能拖動到底部,html頁面不能完全載入,原因在於當前頁面沒有更新size,解決方法引入$ionicScrollDelegate;

在controller裡改變高度的地方呼叫方法:

$ionicScrollDelegate.resize;

//自定義搜尋框

<form>
    <div class="bar bar-header item-input-inset" style="">
    <i class="icon ion-android-search" style="position: absolute;color: #fdfdfd;font-size: 1.4em;top: 0px;margin-left: 4px;" ng-click="searchSelect()"></i>
    <div style="background-color:#fdfdfd;border-radius: 10px;width: 120%;">
      <label class="item-input-wrapper" id="search-input" style="background-color: #202020;margin:1px 1px 1px 1px;border-radius: 10px">
        <input style="padding-top: 0px;padding-bottom: 2px;margin-left:8px;color:#c4c7ca" type="search" placeholder="大家都在搜..." id=input1 ng-model="NO01">
      </label></div>
      <!-- <button class="button button-clear" style="color: #fefdfb" ng-click="searchSelect()">GO</button> -->
    </div>
</form>

3:上拉(或者當前頁資料不足一頁時)無限請求:

//html程式碼

<ion-list>

<ion-item class="" ng-repeat="item in items track by $index ">{{item.something}}</ion-item>

<ion-item><p style="text-align: center;" ng-if="!noMore">載入中。。。。。</p><p style="text-align: center;" ng-if="noMore">已載入全部資料。。。。。</p></ion-item>
    </ion-list>
    <ion-infinite-scroll on-infinite="loadmore();" icon="ion-load-a" ng-if="!noMore" distance="25px" ></ion-infinite-scroll>
        </ion-content>

//js程式碼

$scope.loadmore = function(){

$http(mypost)        //mypost為http頭
.success(function(data){if (data.length<1) {
    console.log('到底了');
    $scope.noMore=true;
  }else{
    $scope.noMore=false;
  $scope.items = $scope.items.concat(data);
  console.log('data:',data);
  };

}).error(function(err){
  console.log(err);
})  

}

4:設定ng-repeat="item in items"迴圈陣列顯示的最大值(小於items.length):

設定過濾器 limitTo:

如:ng-repeat="item in items|limitTo:10“;

5:js時間:
var myDate = new Date();
myDate.getYear(); //獲取當前年份(2位)
myDate.getFullYear(); //獲取完整的年份(4位,1970-????)
myDate.getMonth(); //獲取當前月份(0-11,0代表1月)
myDate.getDate(); //獲取當前日(1-31)
myDate.getDay(); //獲取當前星期X(0-6,0代表星期天)
myDate.getTime(); //獲取當前時間(從1970.1.1開始的毫秒數)
myDate.getHours(); //獲取當前小時數(0-23)
myDate.getMinutes(); //獲取當前分鐘數(0-59)
myDate.getSeconds(); //獲取當前秒數(0-59)
myDate.getMilliseconds(); //獲取當前毫秒數(0-999)
myDate.toLocaleDateString(); //獲取當前日期
var mytime=myDate.toLocaleTimeString(); //獲取當前時間
myDate.toLocaleString( ); //獲取日期與時間

6:倒計時

//倒計時,需要注入$interval,參考一篇關於倒數60秒重新發送驗證碼的文章: http://www.codesec.net/view/406326.html
  var interval = 1000;
function ShowTime(year,month,day,ho,mi,se)
{
var now = new Date();
var endDate = new Date(year, month-1, day,ho,mi,se);
var leftTime=endDate.getTime()-now.getTime();
var leftsecond = parseInt(leftTime/1000);
var day1=Math.floor(leftsecond/(60*60*24));
var hour=Math.floor((leftsecond-day1*24*60*60)/3600);
var minute=Math.floor((leftsecond-day1*24*60*60-hour*3600)/60);
var second=Math.floor(leftsecond-day1*24*60*60-hour*3600-minute*60);
console.log(day1,'天',hour,'小時',minute,'分鐘',second,'秒');
}
var timer = $interval(function(){
  ShowCountDown(2017,3,17,17,0,0);
},1000);

7:有頂部tabs的頁面(也帶底部tabs)跳轉到二級頁面返回出現頂部tabs不能置頂的情況:在頂部tabs上方空出了一段nav-bar大小的空白

//需要指定class

<ion-tabs class="tabs-striped tabs-top tabs-color-stable"  ng-class="{'tabs-item-hide': $root.hideTabs}">

8:splash screen 在andriod中失效,顯示黑屏:(該問題解決方案來著:“www.”+“wtoutiao.co”+m/p/1eek75z+“.html” ;原文中還提及content阻尼彈回效果,controller傳值,安卓版本釋出等多個問題的解決方案!)
主檢視容器ion-nav-view是空的,而它的背景色是#000,所以修復方法是給裡面塞個ion-view

<!-- 內容 -->
<ion-nav-view>
    <!-- 防止啟動時黑屏 -->
    <ion-view></ion-view>
</ion-nav-view>

或者添css,把ion-nav-view的背景色改成白色。但問題還沒解決,黑屏問題變成白屏問題了,解決方案比較麻煩

  1. 把splashscreen外掛降級到v2.0.0

    v2.0.0之後的版本有bug,目前(2016/1/9)自帶的版本是v3.0.0。先cd到專案資料夾,然後命令列:

    // 刪掉現有版本
    cordova plugin rm cordova-plugin-inappbrowser
    // 安裝v2.0.0
    cordova plugin add cordova-plugin-inappbrowser
  2. 改配置檔案MyApp/config.xml

    <preference name="SplashScreen" value="screen"/>
    <preference name="AutoHideSplashScreen" value="false"/>
    <preference name="auto-hide-splash-screen" value="false"/>
    <preference name="ShowSplashScreenSpinner" value="false"/>
    <preference name="SplashMaintainAspectRatio" value="true" />
    <preference name="SplashShowOnlyFirstTime" value="false"/>
    <preference name="SplashScreenDelay" value="10000"/>

    取消自動隱藏(改為程式碼控制隱藏),把持續時間改為較大的值(10秒),設定每次開啟應用都顯示splash screen

    P.S.預設只有SplashScreenSplashScreenDelay,需要把其它的(SplashMaintainAspectRatio可選)都添上

  3. 改app.js

    手動隱藏splash screen,在run裡面添上

    .run(['$rootScope', function($rootScope) {
            // init
            // $rootScope.isLoading = false;
    
            // hide splash immediately
            if(navigator && navigator.splashscreen) {
                navigator.splashscreen.hide();
            }
        });
    }])

9:ionic andriod機型部分視訊無法在video標籤播放

// 可以通過iframe標籤實現

<iframe ng-src="{{targetUrl}}" height="240px" width="100%" autostart="false" ng-if="isAndroid"></iframe>
      <video width="100%" height="240" ng-if="!isAndroid" ng-src="{{targetUrl}}" controls></video>

10:跟著上面,在獲取請求來的視訊不能播放,列印問題是"Error: [$interpolate:interr] Can't interpolate: {{item.videostr}} Error: [$sce:insecurl] Blocked loading resource from url not allowed by $sceDelegate policy.  URL: http://player.youku.co....

解決方法是在controller中引入$sce,

然後轉換 videourl=$sce.trustAsResourceUrl(videourl);

11.eUr

//在cordova開發中如果出現 ERROR Internal navigation rejected - <allow-navigation> not set for url='xxx' 錯誤。(白名單)

 在config.xml檔案中配置  

<allow-navigationhref="*"/>

12.設定了ng-if 後部分Android機型還是不能顯示slide-box裡新載入的圖片及內容的問題

//  實測在低版本Android機型上,使用了ng-if還是不能載入 ng-repeat出來的新載入的內容圖片

//解決方案:在控制器引入$IonicSlideBoxDelegate;

在完成get/post請求獲取伺服器圖片內容後  執行   $ionicSlideBoxDelegate.update();