AngularJS Cordova插件介绍

素颜马尾好姑娘i 2022-08-10 16:53 388阅读 0赞

#

  1. [ngCordova][]是在Cordova Api基础上封装的一系列开源的AngularJs服务和扩展,让开发者可以方便的在HybridApp开发中调用设备能力,即可以在AngularJs代码中访问设备能力Api
  2. 根据我的经验,在cordova插件的sucesserror js回调方法中,是无法使用 angularjs$scope对象和注入的方法的,只能访问全局的方法和变量,这样会导致很多麻烦,必须使用传统的js方法写很多难看的代码。使用ngCordova应该可以解决这个问题。
  3. 目前ngCordova提供了二维码扫描,摄像头,联系人,设备信息,网络信息等插件的支持。

下面是ngCordova示例和文档的简单介绍。

Examples and Docs

To use any of the plugin wrappers below, all you need to do is link to the ng-cordova.js file in your app. Then, include ngCordova as a dependency in your angular module:

  1. angular.module('myApp', ['ngCordova'])

NOTE: Include ng-cordova.js in your index.html file before cordova.js:

  1. <script src="lib/ngCordova/dist/ng-cordova.js"></script>
  2. <script src="cordova.js"></script>

$cordovaBarcodeScanner

The Barcode Scanner Plugin opens a camera view and automagically scans a barcode, returning the data back to you. View Official Docs

  1. cordova plugin add https://github.com/wildabeast/BarcodeScanner.git
  2. module.controller('BarcodeScannerCtrl', function($scope, $cordovaBarcodeScanner) {
  3. $scope.scanBarcode = function() {
  4. $cordovaBarcodeScanner.scan().then(function(imageData) {
  5. // Success! Barcode data is here
  6. }, function(err) {
  7. // An error occured. Show a message to the user
  8. });
  9. };
  10. // NOTE: encoding not functioning yet
  11. $scope.encodeData = function() {
  12. $cordovaBarcodeScanner.encode(BarcodeScanner.Encode.TEXT_TYPE, "http://www.nytimes.com").then(function(success) {
  13. // Success!
  14. }, function(err) {
  15. // An error occured. Show a message to the user
  16. });
  17. }
  18. });

$cordovaCamera

This service makes it easy to use the org.apache.cordova.camera plugin to take pictures and video from a device. View Official Docs

  1. cordova plugin add org.apache.cordova.camera
  2. module.controller('PictureCtrl', function($scope, $cordovaCamera) {
  3. $scope.takePicture = function() {
  4. var options = {
  5. quality : 75,
  6. destinationType : Camera.DestinationType.DATA_URL,
  7. sourceType : Camera.PictureSourceType.CAMERA,
  8. allowEdit : true,
  9. encodingType: Camera.EncodingType.JPEG,
  10. targetWidth: 100,
  11. targetHeight: 100,
  12. popoverOptions: CameraPopoverOptions,
  13. saveToPhotoAlbum: false
  14. };
  15. $cordovaCamera.getPicture(options).then(function(imageData) {
  16. // Success! Image data is here
  17. }, function(err) {
  18. // An error occured. Show a message to the user
  19. });
  20. }
  21. });

View Camera Options

$cordovaContacts

A powerful way to create, remove, and search through contacts on the device.

  1. cordova plugin add org.apache.cordova.contacts
  2. module.controller('MyCtrl', function($scope, $cordovaContacts) {
  3. $scope.addContact = function() {
  4. $cordovaContacts.save($scope.contactForm).then(function(result) {
  5. // Contact saved
  6. }, function(err) {
  7. // Contact error
  8. });
  9. };
  10. // Many more features will be added shortly
  11. });

$cordovaDevice

Grab device related information, such as platform, and device model.

  1. cordova plugin add org.apache.cordova.device
  2. module.controller('MyCtrl', function($scope, $cordovaDevice) {
  3. var device = $cordovaDevice.getDevice();
  4. var cordova = $cordovaDevice.getCordova();
  5. var model = $cordovaDevice.getModel();
  6. var platform = $cordovaDevice.getPlatform();
  7. var uuid = $cordovaDevice.getUUID();
  8. var version = $cordovaDevice.getVersion();
  9. });

$cordovaDeviceMotion

Get access to the device’s accelerometer. View Official Docs

  1. cordova plugin add org.apache.cordova.device-motion
  2. module.controller('DeviceMotionCtrl', function($scope, $cordovaDeviceMotion) {
  3. var watch;
  4. $scope.getAcceleration = function () {
  5. $cordovaDeviceMotion.getCurrentAcceleration().then(function(result) {
  6. // Success!
  7. }, function(err) {
  8. // An error occured. Show a message to the user
  9. });
  10. };
  11. $scope.watchAcceleration = function () {
  12. var options = { frequency: 1000 }; // Update every 1 second
  13. watch = $cordovaDeviceMotion.watchAcceleration(options);
  14. watch.promise.then(
  15. function() { /* unused */},
  16. function(err) {},
  17. function(acceleration) {
  18. $cordovaDialogs.alert('Acceleration X: ' + acceleration.x + '\n' +
  19. 'Acceleration Y: ' + acceleration.y + '\n' +
  20. 'Acceleration Z: ' + acceleration.z + '\n' +
  21. 'Timestamp: ' + acceleration.timestamp + '\n');
  22. });
  23. };
  24. $scope.clearWatch = function() {
  25. // use watchID from watchAccelaration()
  26. if(!watch) { return; }
  27. $cordovaDeviceMotion.clearWatch(watch.watchId).then(function(result) {
  28. // Success!
  29. }, function(err) {
  30. // An error occured. Show a message to the user
  31. });
  32. }
  33. });

$cordovaDeviceOrientation

Get access to the device’s compass. View Official Docs

  1. cordova plugin add org.apache.cordova.device-orientation
  2. module.controller('DeviceOrientationCtrl', function($scope, $cordovaDeviceOrientation) {
  3. $cordovaDeviceOrientation.getCurrentHeading().then(function(result) {
  4. // Success!
  5. }, function(err) {
  6. // An error occured. Show a message to the user
  7. });
  8. var options = { frequency: 1000 }; // Update every 1 second
  9. var watch = $cordovaDeviceOrientation.watchHeading(options);
  10. watch.promise.then(function(result) { /* unused */ },
  11. function(err) {
  12. // An error occured. Show a message to the user
  13. }, function(position) {
  14. // Heading comes back in
  15. // position.magneticHeading
  16. });
  17. $cordovaDeviceOrientation.clearWatch(watch.watchId).then(function(result) {
  18. // Success!
  19. }, function(err) {
  20. // An error occured. Show a message to the user
  21. });
  22. });

$cordovaDialogs

Trigger alert, confirm, and prompt windows, or send beeps (beep, beep!)

  1. cordova plugin add org.apache.cordova.dialogs
  2. module.controller('MyCtrl', function($scope, $cordovaDialogs) {
  3. $cordovaDialogs.alert('Wow!');
  4. $cordovaDialogs.confirm('Are you sure?');
  5. $cordovaDialogs.prompt('Please Login');
  6. // beep 3 times
  7. $cordovaDialogs.beep(3);
  8. });

$cordovaFile

A Plugin to get access to the device’s files and directories. View Official Docs

  1. cordova plugin add org.apache.cordova.file
  2. module.controller('MyCtrl', function($scope, $cordovaFile) {
  3. $cordovaFile.checkDir(directory).then(function(result) {
  4. // Success!
  5. }, function(err) {
  6. // An error occured. Show a message to the user
  7. });
  8. // parameters: directory, replace (boolean)
  9. $cordovaFile.createDir(directory, false).then(function(result) {
  10. // Success!
  11. }, function(err) {
  12. // An error occured. Show a message to the user
  13. });
  14. $cordovaFile.checkFile(directory, file).then(function(result) {
  15. // Success!
  16. }, function(err) {
  17. // An error occured. Show a message to the user
  18. });
  19. // parameters: directory, file, replace (boolean)
  20. $cordovaFile.createFile(directory, file, true).then(function(result) {
  21. // Success!
  22. }, function(err) {
  23. // An error occured. Show a message to the user
  24. });
  25. $cordovaFile.removeFile(directory, file).then(function(result) {
  26. // Success!
  27. }, function(err) {
  28. // An error occured. Show a message to the user
  29. });
  30. // doesn't function at the moment
  31. $cordovaFile.writeFile(directory, file).then(function(result) {
  32. // Success!
  33. }, function(err) {
  34. // An error occured. Show a message to the user
  35. });
  36. // Reads a file as TEXT
  37. $cordovaFile.readFile(directory, file).then(function(result) {
  38. // Success!
  39. }, function(err) {
  40. // An error occured. Show a message to the user
  41. });
  42. // parameters: source, filePath, trust all hosts (boolean), options
  43. $cordovaFile.downloadFile(source, filePath, true, options).then(function(result) {
  44. // Success!
  45. }, function(err) {
  46. // An error occured. Show a message to the user
  47. });
  48. // parameters: source, filePath, options
  49. $cordovaFile.uploadFile(server, filePath, options).then(function(result) {
  50. // Success!
  51. }, function(err) {
  52. // An error occured. Show a message to the user
  53. });
  54. });

$cordovaGeolocation

Grab the current location of the user, or grab continuous location changes: View Official Docs

  1. cordova plugin add org.apache.cordova.geolocation
  2. module.controller('MyCtrl', function($scope, $cordovaGeolocation) {
  3. $cordovaGeolocation.getCurrentPosition().then(function(position) {
  4. // Position here: position.coords.latitude, position.coords.longitude
  5. }, function(err) {
  6. // error
  7. });
  8. var watch = $cordovaGeolocation.watchPosition({
  9. frequency: 1000
  10. });
  11. watch.promise.then(function() {
  12. // Not currently used
  13. }, function(err) {
  14. // An error occured. Show a message to the user
  15. }, function(position) {
  16. // Active updates of the position here
  17. // position.coords.latitude/longitude
  18. });
  19. });

$cordovaGlobalization

Obtains information and performs operations specific to the user’s locale and timezone. View Official Docs

  1. cordova plugin add org.apache.cordova.globalization
  2. module.controller('MyCtrl', function($scope, $cordovaGlobalization) {
  3. $cordovaGlobalization.getPreferredLanguage().then(
  4. function(result) {
  5. // result
  6. },
  7. function(error) {
  8. // error
  9. });
  10. $cordovaGlobalization.getLocaleName().then(
  11. function(result) {
  12. // result
  13. },
  14. function(error) {
  15. // error
  16. });
  17. $cordovaGlobalization.getFirstDayOfWeek().then(
  18. function(result) {
  19. // result
  20. },
  21. function(error) {
  22. // error
  23. });
  24. // Soon implemented:
  25. // dateToString
  26. // stringToDate
  27. // getDatePattern
  28. // getDateNames
  29. // isDayLightSavingsTime
  30. // numberToString
  31. // stringToNumber
  32. // getNumberPattern
  33. // getCurrencyPattern
  34. });

$cordovaNetwork

Check network connection types, and track offline and online status. View Official Docs

  1. cordova plugin add org.apache.cordova.network-information
  2. module.controller('MyCtrl', function($scope, $cordovaNetwork) {
  3. var type = $cordovaNetwork.getNetwork();
  4. var isOnline = $cordovaNetwork.isOnline();
  5. var isOffline = $cordovaNetwork.isOffline();
  6. });

View Network Types

$cordovaPinDialog

Numeric password dialog.

  1. cordova plugin add https://github.com/Paldom/PinDialog.git
  2. module.controller('MyCtrl', function($scope, $cordovaPush) {
  3. $cordovaPinDialog.prompt('Some message here').then(
  4. function(result) {
  5. // result
  6. },
  7. function (error) {
  8. // error
  9. })
  10. });

$cordovaPush

Allows your application to receive push notifications View Official Docs

  1. cordova plugin add https://github.com/phonegap-build/PushPlugin.git
  2. module.controller('MyCtrl', function($scope, $cordovaPush) {
  3. var androidConfig = {
  4. "senderID":"replace_with_sender_id",
  5. "ecb":"onNotification"
  6. };
  7. var iosConfig = {
  8. "badge":"true",
  9. "sound":"true",
  10. "alert":"true",
  11. "ecb":"onNotificationAPN"
  12. };
  13. $cordovaPush.register(config).then(function(result) {
  14. // Success!
  15. }, function(err) {
  16. // An error occured. Show a message to the user
  17. });
  18. $cordovaPush.unregister(options).then(function(result) {
  19. // Success!
  20. }, function(err) {
  21. // An error occured. Show a message to the user
  22. });
  23. // iOS only
  24. $cordovaPush.setBadgeNumber(2).then(function(result) {
  25. // Success!
  26. }, function(err) {
  27. // An error occured. Show a message to the user
  28. });
  29. });

$cordovaSocialSharing

Social Sharing plugin.

  1. cordova plugin add https://github.com/EddyVerbruggen/SocialSharing-PhoneGap-Plugin.git
  2. module.controller('MyCtrl', function($scope, $cordovaSocialSharing) {
  3. $cordovaSocialSharing.shareViaTwitter(message, image, link).then(function(result) {
  4. // Success!
  5. }, function(err) {
  6. // An error occured. Show a message to the user
  7. });
  8. $cordovaSocialSharing.shareViaWhatsApp(message, image, link).then(function(result) {
  9. // Success!
  10. }, function(err) {
  11. // An error occured. Show a message to the user
  12. });
  13. $cordovaSocialSharing.shareViaFacebook(message, image, link).then(function(result) {
  14. // Success!
  15. }, function(err) {
  16. // An error occured. Show a message to the user
  17. });
  18. // access multiple numbers in a string like: '0612345678,0687654321'
  19. $cordovaSocialSharing.shareViaSMS(message, number).then(function(result) {
  20. // Success!
  21. }, function(err) {
  22. // An error occured. Show a message to the user
  23. });
  24. // TO, CC, BCC must be an array, Files can be either null, string or array
  25. $cordovaSocialSharing.shareViaEmail(message, subject, toArr, bccArr, file).then(
  26. function(result) {
  27. // Success!
  28. }, function(err) {
  29. // An error occured. Show a message to the user
  30. });
  31. $cordovaSocialSharing.canShareVia(socialType, message, image, link).then(
  32. function(result) {
  33. // Success!
  34. }, function(err) {
  35. // An error occured. Show a message to the user
  36. });
  37. });

$cordovaSpinnerDialog

A dialog with a spinner wheel. View Official Docs

  1. cordova plugin add https://github.com/Paldom/SpinnerDialog.git
  2. module.controller('MyCtrl', function($scope, $cordovaSpinnerDialog) {
  3. // Show spinner dialog with message
  4. // Title and message only works on Android
  5. $cordovaSpinnerDialog.show("title","message");
  6. // Hide spinner dialog
  7. $cordovaSpinnerDialog.hide();
  8. });

$cordovaSplashscreen

Show or hide the Splash Screen.

  1. cordova plugin add org.apache.cordova.splashscreen
  2. module.controller('MyCtrl', function($scope, $cordovaSplashscreen) {
  3. $cordovaSplashscreen.show();
  4. });

$cordovaStatusbar

Configure the device’s StatusBar with colors and styles.

  1. cordova plugin add org.apache.cordova.statusbar
  2. module.controller('MyCtrl', function($scope, $cordovaStatusbar) {
  3. $cordovaStatusbar.overlaysWebView(true);
  4. // styles: Default : 0, LightContent: 1, BlackTranslucent: 2, BlackOpaque: 3
  5. $cordovaStatusbar.style(1);
  6. // supported names: black, darkGray, lightGray, white, gray, red, green,
  7. // blue, cyan, yellow, magenta, orange, purple, brown
  8. $cordovaStatusbar.styleColor('black');
  9. $cordovaStatusbar.styleHex('#000');
  10. $cordovaStatusbar.hide();
  11. $cordovaStatusbar.show();
  12. var isVisible = $cordovaStatusbar.isVisible();
  13. });

$cordovaToast

This plugin allows you to show a native Toast (a little text popup) on iOS, Android and WP8. It’s great for showing a non intrusive native notification which is guaranteed always in the viewport of the browser.View Official Docs

  1. cordova plugin add https://github.com/EddyVerbruggen/Toast-PhoneGap-Plugin.git

You have two choices to make when showing a Toast: where to show it and for how long.

  • show(message, duration, position)

    • duration: ‘short’, ‘long’
    • position: ‘top’, ‘center’, ‘bottom’

You can also use any of these convenience methods:

  • showShortTop(message)
  • showShortCenter(message)
  • showShortBottom(message)
  • showLongTop(message)
  • showLongCenter(message)
  • showLongBottom(message)

    module.controller(‘MyCtrl’, function($scope, $cordovaVibration) {

    $cordovaToast.show(‘Here is a message’, ‘long’, ‘center’).then(function(success) {

    1. // success

    }, function (error) {

    1. // error

    });

    $cordovaToast.showShortTop(‘Here is a message’).then(function(success) {

    1. // success

    }, function (error) {

    1. // error

    });

    $cordovaToast.showLongBotton(‘Here is a message’).then(function(success) {

    1. // success

    }, function (error) {

    1. // error

    });

    });

$cordovaVibration

Vibrate the device programatically. View Official Docs

  1. cordova plugin add org.apache.cordova.vibration
  2. module.controller('MyCtrl', function($scope, $cordovaVibration) {
  3. // Vibrate 100ms
  4. $cordovaVibration.vibrate(100);
  5. });

$cordovaCapture

This plugin allows you to record sound, video and images throught the native capabilities of the deviceView Official Docs

  1. cordova plugin add org.apache.cordova.media-capture
  2. module.controller('MyCtrl', function($scope, $cordovaCapture) {
  3. $scope.captureAudio = function() {
  4. var options = { limit: 3, duration: 10 };
  5. $cordovaCapture.captureAudio(options).then(function(audioData) {
  6. // Success! Audio data is here
  7. }, function(err) {
  8. // An error occured. Show a message to the user
  9. });
  10. }
  11. $scope.captureImage = function() {
  12. var options = { limit: 3 };
  13. $cordovaCapture.captureImage(options).then(function(imageData) {
  14. // Success! Image data is here
  15. }, function(err) {
  16. // An error occured. Show a message to the user
  17. });
  18. }
  19. $scope.captureVideo = function() {
  20. var options = { limit: 3, duration: 15 };
  21. $cordovaCapture.captureVideo(options).then(function(videoData) {
  22. // Success! Video data is here
  23. }, function(err) {
  24. // An error occured. Show a message to the user
  25. });
  26. }
  27. });

发表评论

表情:
评论列表 (有 0 条评论,388人围观)

还没有评论,来说两句吧...

相关阅读

    相关 使用swift开发Cordova

    最近研究了用swift开发cordova插件的问题,事实证明用swift开发cordova插件是完全可行的,不要再去折腾烦人的oc代码了!主要参考了一个地理围栏插件 https