错误:“选择器未实现”

时间:2012-11-20 19:39:06

标签: angularjs

我在选择带角度的元素时遇到了困难。我一定是犯了一些愚蠢的错误:

function Root($scope) {
    $scope.elem = angular.element('div');
}​

导致的错误:

Error: selectors not implemented
    at Error (<anonymous>)
    at Object.JQLite [as element] (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:1523:13)
    at new Root (http://fiddle.jshell.net/kzX9z/7/show/:28:27)
    at invoke (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:2795:28)
    at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:2805:23)
    at $get (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:4621:24)
    at $get.i (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:4200:17)
    at forEach (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:117:20)
    at nodeLinkFn (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:4185:11)
    at compositeLinkFn (https://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.js:3834:14) angular.js:5563
$get angular.js:5563
$get angular.js:4661
$get.Scope.$apply angular.js:7890
SNAKE_CASE_REGEXP angular.js:930
invoke angular.js:2788
bootstrap angular.js:928
angularInit angular.js:904
(anonymous function) angular.js:14397
trigger angular.js:1695
event.preventDefault angular.js:1930
forEach angular.js:110
eventHandler

Reproduced in this fiddle

我做错了什么?我包括了jQuery。这不应该起作用吗?

1 个答案:

答案 0 :(得分:60)

这是因为jquery需要在 angular之前引用。 JSFiddle注册了Angular 1.0.2 FIRST,然后注册了你作为资源添加的jquery verson。

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>

这样Angular知道它可以使用JQuery而不是JQLite。完成此操作后,选择器将在angular.element()

中运行

Here's your fiddle, updated to reference the scripts in the right order (and altered slightly, see the comments)

如果您没有JQuery,可以手动选择DOM元素。

var div = document.querySelector('div');
$scope.elem = angular.element(div);