2016-03-31 10 views
0

Ionic & Cordovaで写真編集アプリを作成しています。オンラインでAngularJS変数を取得してJSで使用することができます。しかし、私はそれを試してみましたが、私は、コンソールにエラーを取得しています: app.js:1271キャッチされない例外TypeError:$(...)をスコープはここで機能AngularJSから変数を取得し、javascriptで変数を使用する方法は?

ではない私のコードは次のとおりです。

//Loading the variable from AngularJS 
     window.onload = function() { 
      var sources = { 

       yoda: $('[ng-controller="SecureController"]').scope().pictureUrl 
      }; 
      loadImages(sources, initStage); 

     }; 


//Using ngCordova to capture the image and set the image link to $scope.pictureUrl. *This part works* 

$cordovaCamera.getPicture(options) 
     .then(function(imageData) { 
      syncArray.$add({image: imageData}); 
      $scope.pictureUrl= 'data:image/jpeg:base64,' + data 
      .then(function() { 
       alert("Image has been uploaded"); 
      }); 
+0

もっとコードを表示できますか?これはコントローラーかサービスか? $ scopeを依存関係として定義していない可能性があります。 – Dexter

+1

下の私の答えを参照してください –

答えて

1

あなたのjQueryリソースリファレンスがあなたのangualrjsリソース参照の後にあり、角スイッチが角度jqliteに切り替わり、したがってjQueryセレクタがわからない場合.scope()

私は以下の3つのjsfiddleを作成して、コンソールにエラーがあります:

Example1: - :

<div id="myDiv" ng-controller="SecureController"> 
    <h1>{{pictureUrl}}</h1> 
</div> 

JS:いいえjQueryが何jqueryのは、あなたが角度jqliteに

HTMLを使用する必要が参照されていない場合

var myApp = angular.module('application', []); 

myApp.controller('SecureController', ['$scope', function($scope) { 
    $scope.pictureUrl = 'this is a test url'; 
}]); 

window.onload = function() { 
    var sources = { 
    yoda: angular.element(document.getElementById('myDiv')).scope().pictureUrl 
    }; 
    console.log(sources.yoda); 
    alert(sources.yoda); 
} 

Example2

:jQueryのは、角の前に含まれています - jQueryセレクタを使用する

HTML:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script> 

<div id="myDiv" ng-controller="SecureController"> 
    <h1>{{pictureUrl}}</h1> 
</div> 

JS:

var myApp = angular.module('application', []); 

myApp.controller('SecureController', ['$scope', function($scope) { 
    $scope.pictureUrl = 'this is a test url'; 
}]); 

window.onload = function() { 
    var sources = { 
    yoda: $('#myDiv').scope().pictureUrl 
    }; 
    console.log(sources.yoda); 
    alert(sources.yoda); 
} 

Example3: - :

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js"></script> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> 

<div id="myDiv" ng-controller="SecureController"> 
    <h1>{{pictureUrl}}</h1> 
</div> 

JS:

var myApp = angular.module('application', []); 

myApp.controller('SecureController', ['$scope', function($scope) { 
    $scope.pictureUrl = 'this is a test url'; 
}]); 

window.onload = function() { 
    var sources = { 
    yoda: $('#myDiv').scope().pictureUrl 
    }; 
    console.log(sources.yoda); 
    alert(sources.yoda); 
} 
jQueryのangaular後に含まれているあなたは

HTML Uncaught TypeError: $(...).scope is not a functionエラーになります

+0

いいえ。答えをありがとう、しかし私は私のスクリプトをチェックします。私が言ったように、私はIonic Frameworkでコーディングしているので、インデックスページに参​​照があるようにしています。とにかく、私は確かに確認し、それに応じてあなたに戻ってきます –

関連する問題