【发布时间】:2015-05-03 08:25:01
【问题描述】:
我正在尝试使用 queryselectorAll 来获取具有特定类名的所有元素。我有一个问题,我只能从 index.html 页面中的元素获取结果。如果我尝试从部分(ng-view)html 页面获取节点列表,我会得到一个空结果。
App.js
var myApp = angular.module('myApp', ['ngRoute', 'articleControllers']);
myApp.config(['$routeProvider', function($routeProvider){
$routeProvider.
when('/main', {
templateUrl: 'lib/partials/main.html',
controller: 'MainController'
})
}]);
controller.js
var articleControllers = angular.module('articleControllers', []);
articleControllers.controller('MainController', ['$scope', '$http', function ($scope, $http){
$http.get('http://...').success(function(data) {
$scope.articles = JSON.parse(data);
});
}]);
index.html
(body, header, ...)
<section ng-view>
</section>
(footer, ...)
lib/partials/main.html
...
<div class="nextArticle">
<button class="next_btn" title="View next article"> link text</button>
</div>
...
最后:helper.js(这是我调用的脚本,就像 index.html 中的所有其他脚本一样)
var elList = document.querySelectorAll('.next_btn');
Array.prototype.forEach.call(elList, function(el) {
console.log("Found: " + el);
});
所以回顾一下: 它就像 querySelectorAll 只能在 index.html 中找到元素,而不是在带有 ng-view 的部分视图中。 有人知道可能出了什么问题吗? 我正在尝试不使用 jquery。
【问题讨论】:
标签: javascript html angularjs selectors-api