How to integrate Phonegap Camera with AngularJS

前端 未结 2 1888
渐次进展
渐次进展 2021-02-06 02:51

I\'m trying to figure out the best practice for integrating the phonegap camera with AngularJS. The first method I tried was creating a factory with promises that gets called f

2条回答
  •  自闭症患者
    2021-02-06 03:07

    Personally I would place the logic in a directive, since it will need to access DOM functions (and directives are better suited for that). If you use require: 'ngModel' in your directive, you can use it to store the output value.

    Html:

    
    

    Directive:

    app.directive('camera', function() {
       return {
          restrict: 'A',
          require: 'ngModel',
          link: function(scope, elm, attrs, ctrl) {
             elm.on('click', function() {
                navigator.camera.getPicture(function (imageURI) {
                   scope.$apply(function() {
                      ctrl.$setViewValue(imageURI);
                   });
                }, function (err) {
                   ctrl.$setValidity('error', false);
                }, { quality: 50, destinationType: Camera.DestinationType.FILE_URI }
             });
          }
       };
    });
    

    In your controller, you can $watch the model and push it into an array:

    $scope.myPictures = [];
    $scope.$watch('myPicture', function(value) {
       if(value) {
          myPictures.push(value);
       }
    }, true);
    

提交回复
热议问题