我想更改标题div的默认内容:
<div>
<div>
<div class="page-header">
<h1><span>{{messages['default_title']}}</span></h1>
</div>
<div>
<!-- other elements-->
<div>
在我的控制器中,我写了这个:
var titleElement = angular.element('div.page-header');
titleElement.html('<h1><span>Hello</span></h1>');
div的内容成功更改:
<div class="page-header"><h1><span>Hello</span></h1></div>
但我收到此错误,并且无法成功加载页面:
TypeError: Cannot read property 'childNodes' of undefined
at g (https://localhost/test/js/angular/angular.min.js:47:278)
at g (https://localhost/test/js/angular/angular.min.js:47:273)
at g (https://localhost/test/js/angular/angular.min.js:47:273)
at g (https://localhost/test/js/angular/angular.min.js:47:273)
at g (https://localhost/test/js/angular/angular.min.js:47:273)
at https://localhost/test/js/angular/angular.min.js:46:377
at link (https://localhost/test/js/angular/angular-route.js:915:7)
at J (https://localhost/test/js/angular/angular.min.js:54:373)
at g (https://localhost/test/js/angular/angular.min.js:47:256)
at https://localhost/test/js/angular/angular.min.js:46:377 <div ng-view="" class="ng-scope">
答案 0 :(得分:2)
而不是操纵页面标题。
尝试更新控制器中的变量
$scope.messages['default_title'] = "Hello";
答案 1 :(得分:1)
angular.element
接受一串HTML或JS元素,而不是选择器
var titleElement = angular.element(document.querySelector('.page-header'));
titleElement.html('<h1><span>Hello</span></h1>');