如果你的jQuery資源引用您的angualrjs資源引用後,您會收到此錯誤,角度切換到角jqlite,因而jQuery選擇不知道.scope()
我創建了以下三個jsfiddles來證明,例如3將在控制檯中有錯誤:
Example1:沒有jQuery的 - 如果沒有jQuery是引用您必須使用角度jqlite
HTML:
<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: 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:jQuery是包括後angaular - 您將得到Uncaught TypeError: $(...).scope is not a function
錯誤
HTML:
<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);
}
你能展示更多的代碼嗎?這是在控制器還是服務?這可能只是因爲你沒有將$ scope定義爲依賴項。 – Dexter
請在下面看到我的回答 –