あなたの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
エラーになります
もっとコードを表示できますか?これはコントローラーかサービスか? $ scopeを依存関係として定義していない可能性があります。 – Dexter
下の私の答えを参照してください –