如果你的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
请在下面看到我的回答 –