Ionic+ngCorodva 實(shí)現(xiàn)手機(jī)震動(dòng)功能


ngCordova安裝

  • 安裝bower(方便安裝ngCordova):npm install -g bower
  • 安裝ngCordova: bower install ngCordova

提示: 對(duì)于ENOGIT git is not installed or not in the PATH的錯(cuò)誤,是因?yàn)闆]有找到git,你需要:

  1. 安裝git,通過git bash執(zhí)行上述命令
  2. 如果已經(jīng)安裝了git,可以配置git環(huán)境變量, 如:C:\Program Files (x86)\Git\bin;C:\Program Files (x86)\Git\cmd 加入path,配置完成,重新啟動(dòng)cmd

安裝震動(dòng)插件:

  • 進(jìn)入項(xiàng)目文件執(zhí)行:
//查看目錄testApp(項(xiàng)目名稱)\platforms\android\assets\www\plugins\
//有沒有 cordova-plugin-vibration
//有就是添加成功cordova plugin add org.apache.cordova.vibration ```
- **編譯項(xiàng)目:**

![編譯.png](http://upload-images.jianshu.io/upload_images/935378-1051a09d990ec22c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
- 編譯成功后:
``` java
//查看 testApp\platforms\android\AndroidManifest.xml 中有沒有添加
<uses-permission android:name="android.permission.VIBRATE" /> 權(quán)限
//查看 G:\Ionic\testApp\platforms\android\res\xml\config.xml 有沒有
<feature name="Vibration">
<param name="android-package" value="org.apache.cordova.vibration.Vibration" />
</feature>
//有就是添加成功,沒有則需要 重新 build```
##添加代碼
- 編輯controller.js 傳入?yún)?shù) **$cordovaVibration**
``` javascript
.controller('VibrationCtrl', function($scope, $cordovaVibration)
 { 
  //設(shè)置調(diào)用函數(shù) startVib 
  $scope.startVib=function(){ 
    // 震動(dòng) 1000ms 
    $cordovaVibration.vibrate(1000); 
  };
})
  • 編輯app.js,給綁定controller與html
//切記添加參數(shù) ngCordova
angular.module('starter', ['ionic', 'ngCordova','starter.controllers', 'starter.services'])
//找到相應(yīng)的路由
.state('tab.dash', 
{ 
    url: '/dash', 
    views: { 
        'tab-dash': { 
             templateUrl: 'templates/tab-dash.html', 
             //綁定controller 
             controller: 'VibrationCtrl' 
        } 
    } 
})
  • 編輯tab-dash.html
<!-- 設(shè)置點(diǎn)擊的action為函數(shù)startVib( )--> 
<button ng-click='startVib()' class="button button-block button-royal padding-top">震動(dòng)</button>

小結(jié)

至此,震動(dòng)功能就添加成功了,連接真機(jī)測(cè)試即可。
ngCordova中其他插件使用方式與本文相差無幾,可以參考本文章的使用方法

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,534評(píng)論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,261評(píng)論 6 342
  • WebSocket-Swift Starscream的使用 WebSocket 是 HTML5 一種新的協(xié)議。它實(shí)...
    香橙柚子閱讀 24,726評(píng)論 8 183
  • 南澳島是汕頭市隸屬的澄海市附近一個(gè)海島,是全國(guó)第六大海島。昨天和友人帶著兩個(gè)娃來了場(chǎng)說走就走的旅游。 ...
    Candy213閱讀 360評(píng)論 0 1
  • 《查理和巧克力工廠》 電影主要內(nèi)容:威利旺卡是一位偉大的巧克力制造商,且擁有一座神秘的巧克力工廠。一天,他發(fā)布通告...
    _戒糖閱讀 1,412評(píng)論 0 2

友情鏈接更多精彩內(nèi)容