“不要在循环中创建函数。”皮棉错误。写它的正确方法是什么?

时间:2016-01-07 06:14:02

标签: base64 jslint getpicture

控制器代码如下。解决这个问题的正确方法是什么?

$scope.photoData = [];
$cordovaImagePicker.getPictures(options).then(function (results) {
  for (var i = 0; i < results.length; i++) {
    $scope.photoData.push(results[i]);
    console.log(results[i]);
    window.plugins.Base64.encodeFile(photoData, function(base64) {
      console.log('photoData: ' + base64);
    });
  }


  if (!$scope.$$phase) {
    $scope.$apply();
  }
}, function (err) {
  // An error occured. Show a message to the user
});

查看代码

    <ion-slide ng-repeat="item in photoData">
    <img ng-src="data:image/jpg;base64,{{item}}" style="max-width: 100%">
  </ion-slide>

1 个答案:

答案 0 :(得分:0)

您可以在循环开始之前声明该函数

$cordovaImagePicker.getPictures(options).then(function(results) {
    function successFunc(base64) {
        console.log('photoData: ' + base64);
    }

    for (var i = 0; i < results.length; i++) {
        $scope.photoData.push(results[i]);
        console.log(results[i]);
        window.plugins.Base64.encodeFile(photoData, successFunc);
    }
    if (!$scope.$$phase) {
        $scope.$apply();
    }
}, function(err) {
    // An error occured. Show a message to the user
});