加群學習:457351423 這里有4000多部學習視頻,有需要的歡迎進群學習!
1、No Content-Security-Policy meta tag found. Please add one when using the cordova-plugin-whitelist plugin.
解決辦法
index.html 中添加
《meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'"》
2、ionic 隱藏 nav-bar
文檔上來看,需要在controller中調用 $ionicNavBarDelegatehide-nav-bar = "true"
例如 .controller('DashCtrl', function ($scope, $ionicNavBarDelegate) {
$ionicNavBarDelegate.showBar(false);
})
但沒起作用。最后在模板中使用標簽方法
該方法起作用。
3、 ng-model 與 input 值的問題
例如 input type="text" ng-model="querystr"
ng-model綁定到$scope.querystr 時,model會根據 text輸入自動變化,但在controller中將
$scope.querystr=''時 ,text input的值不會產生變化。
並且在使用ng-model時,無法使用
angular.element(document).find(selector).val("some value");方式進行賦值。
解決方法是在controller 中,設置一個結構,如
$scope.queryMode ={querystr:''};
在input 中綁定 ng-model = "queryModel.querystr"
則controller中重設 $scope.queryMode.querystr = '';
綁定生效,input 清空。
4、cordova插件 ,在ripple中彈出錯誤窗口
如果安裝cordova插件,如keyboard,statusbar 等, 會在ripple中拋各種錯,並且每次加載會彈出窗口,讓你寫JSON回調, 這個是ripple的BUG,不支持自定義插件, 去掉彈窗的辦法是在app.js上方,即定義angular.module()的上方
寫
彈窗將不再出現,但輸出窗口中的ripple.js中的錯誤仍會,你可以過濾,也可以不管。
6、ionic關於IFAME的調用
因為使用第三方接口,會造成跑到應用外的地址,ionic無法控制外部連接回到應用,因此一般使用iframe方式
打開外部連接,並加一個headbar用來返回到應用,如在modal中打開外部地址
如html中
《script id="login.html" type="text/ng-template"》
《ion-modal-view》
《ion-header-bar align-title="center" class="bar-positive"》
《div class="buttons"》
《button class="button button-clear button-icon icon ion-ios-arrow-back"》《/button》
《button ng-click="closeModal()" class="button button-clear button-icon icon "》取消《/button》
《/div》
《h1 class="title"》LOGIN《/h1》
《/ion-header-bar》
《ion-content scroll="true" class="has-header no-padding"》
《iframe id="ifmr2" data-tap-disabled="true" ng-src="{{chatStru.paySrc}}"》《/iframe》
《/ion-content》
《/ion-modal-view》
《/script》
controller中
$scope.tt = new Date().getMilliseconds();
$scope.chatStru = {
paySrc: $sce.trustAsResourceUrl('http://xxx.om/testlogin.jsp?tt='+$scope.tt),
token: "",
hasToken: false
};
$ionicModal.fromTemplateUrl('login.html', {
scope: $scope,
animation: 'slide-in-up'
}).then(function (modal) {
$scope.modal = modal;
});
$scope.openModal = function () {
$scope.modal.show();
};
$scope.closeModal = function () {
$scope.modal.hide();
};
在遠端的被iframe的頁面中,加入JS
window.onload=function(){
window.parent.postMessage('logined','*');
}
並在inoic的controller中加入
window.addEventListener('message', function (e) {
var data = e.data;
//這里返回的是 logined,相當於傳遞參數回來。
$scope.modal.hide();
}, false);
你會發現,你即接收到了data,同時又關閉了該modal ,而不需要手動關閉。
最終可以這樣寫
7、使用android studio 運行cordova項目
直接使用platfrom目錄里的gradle 可以在android studio中直接導入 cordova的項目,但運行模擬器時出現
Cannot reload AVD list: cvc-enumeration-valid: Value '280dpi' is not facet-valid with respect to enumeration '[ldpi, mdpi, tvdpi, hdpi, xhdpi, 400dpi, xxhdpi, 560dpi, xxxhdpi]'. It must be a value from the enumeration.
Error parsing D:\sdkforas\android-sdk-windows\system-images\android-22\android-wear\armeabi-v7a\devices.xml
cvc-enumeration-valid: Value '280dpi' is not facet-valid with respect to enumeration '[ldpi, mdpi, tvdpi, hdpi, xhdpi, 400dpi, xxhdpi, 560dpi, xxxhdpi]'. It must be a value from the enumeration.
Error parsing D:\sdkforas\android-sdk-windows\system-images\android-22\android-wear\x86\devices.xml
解決方法 :
用/sdk/tools/lib/devices.xml去替換 system-images\android-22\android-wear\x86\devices.xml和system-images \android-22\android-wear\armeabi-v7a\devices.xml中的devices.xml
8、 android studio 中 gradle 失敗
gradle project sync failed.Basic functionality(e.g.editing,debugging) will not work properly.
解決方法:android studio中,點擊 tools ->Android->sync project with gradles files.
9、加載遠程js或css 出現 Refused to load the script 或 Refused to load the stylesheet
because it violates the following ....
例如 index.html中 加載字體
《link href='http://fonts.useso.com/css?family=RobotoDraft:400,500,700,400italic' rel='stylesheet'》
可在index.html添加安全許可
《meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline' http://fonts.useso.com ; script-src 'self' 'unsafe-inline' 'unsafe-eval';"》
直接加根域名即可,另外不要帶引號 js 需要添加在 script-src 中。
10 、 ionic 中 彈出鍵盤遮擋住輸入框
在config.xml 中修改全屏為FALSE並添加 adjustPan (adjstResize沒有成功)
《preference name="Fullscreen" value="False" /》
《preference name="android-windowSoftInputMode" value="adjustPan"/》
11、ionic監聽滾動
網上的示例xxx.bind('scroll',function(){.....}),很容易將頁面跑死, 換個思路,使用監聽touch
var targetPos = window.screen.availHeight;
$("#scdiv").on("touchend", function () {
if (currpos > 10 && currpos < targetPos / 4) {
$ionicScrollDelegate.$getByHandle('homescroll').scrollTo(0, 0, true);
return false;
} else if (currpos >= targetPos / 4 && currpos <= targetPos - 10) {
$ionicScrollDelegate.$getByHandle('homescroll').scrollTo(0, targetPos - 64, true);
return false;
}
})
當觸摸結束時,判斷當前的位移,來做一些操作。這樣在性能上提高了許多。
$ionicScrollDelegate.$getByHandle 操作的是在html中定義的delegate-handle
如《ion-content id="scdiv" delegate-handle="homescroll"》
12、 APP開啟檢測網絡並提示開啟
需要三個插件
1、https://github.com/apache/cordova-plugin-network-information
2、https://github.com/apache/cordova-plugin-dialogs
3、https://github.com/deefactorial/Cordova-open-native-settings
然后在APP.JS中,divicesReady中
$ionicPlatform.ready(function () {
if (navigator.connection) {
var tmptypes = navigator.connection.type;
if (tmptypes.toUpperCase().indexOf('NONE') > -1 || tmptypes.toUpperCase().indexOf('UNKNOWN') > -1) {
if (navigator.notification) {
navigator.notification.confirm(
'您的設備未開啟網絡',
function (buttonIndex) {
if (buttonIndex == 1) {
if (cordova.plugins.settings) {
cordova.plugins.settings.openSetting("wifi", function () { console.log("network setting openning"); }, function () { console.log("open network setting failed"); });
}
}
}, // callback to invoke with index of button pressed
'提示', // title
['開啟', '取消'] // buttonLabels
);
}
}
}
})
其中openSettings中可設置以下本地設置
var settingNames = array(
13、$sate.go 和 $stateParams 傳參及收參
在 controller1 中使用 $state.go('statename',{id:1}) ;傳遞參數
在 statename 相對應的 controller2 中接收參數
$scope.id = $stateParams.id;
注意,此處必須在router.js 中設置 statename 的參數形式
如.state('statename ', {
url: '/statename ',
params: { 'id': null },
templateUrl: 'templates/Users/statename .html',
controller: 'controller2 '
})
其中params: { 'id': null }, 對應$state傳的參,若不設,則必為undefined
14、關於ionic中想底部加一長按鈕,隨頁面滾動位置不變,例如
最初的方案是在 ion-content 外 ion-view 內添加一層
div .....style= ..... position:fixed bottom:0px;之類,但最終發現,在不同分辨率下位置並不正確
基本可確定是因為statusbar等的緣故,仔細看了下ionic.css最終發現,太簡單了
div class="tabs" 就可以了。
15、php中取值問題
ionic中用的$http method:post ,params........在PHP中用$_POST取不到值, 改成 $_Request 就行了。
16、使用vs2015 release
會提示使用發布配置進行調試時,Android 程序包必須已簽名。要配置 Android 簽名,請按照 http://go.microsoft.com/fwlink/?LinkID=613579 中的說明操作(該網頁很難打開)
實際需要在項目根目錄的build.json中添加生成的keystore
{
"android": {
"release": {
"keystore": "E:\\Projects\\android.keystore",
"storePassword": "*******",
"alias": "********",
"password": "*******",
"keystoreType": ""
}
}
}
至於生成keystore 請百度,只要有jdk 運行命令就OK
17、ionic 的滾動優化
在app.js中的config 塊中,加入
$ionicConfigProvider.scrolling.jsScrolling(false);
默認所有的滾動使用native,會比js的滾動快很多,並且很平滑,
但這樣做的話,無法使用一些效果,如has-bouncing="true"(仿蘋果的一種上下拉顯示背景的彈性效果)
那你可以在特定的view中,ion-content中加入overflow-scroll="false",則該view保持js滾動
18、微信支付
支付寶支付非常簡單,文檔也很豐富,微信的相對來說低了不止一個檔次,當然最郁悶的一件事是
經過了數天的查錯,發現微信的totalfee 居然是以分為單位,換句話來說,你傳價格時,只會有整數
如果出現0.01,那必然出錯。
