gpt4 book ai didi

javascript - 在 AngularJs 中查找元素并更改其内容

转载 作者:行者123 更新时间:2023-11-28 19:17:50 28 4
gpt4 key购买 nike

我想更改标题 div 的默认内容:

<div>
<div>
<div class="page-header">
<h1><span>{{messages['default_title']}}</span></h1>
</div>
<div>
<!-- other elements-->
<div>

在我的 Controller 中,我写了这个:

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">

最佳答案

而不是操纵页面标题。

尝试更新 Controller 中的变量

$scope.messages['default_title'] = "Hello";

关于javascript - 在 AngularJs 中查找元素并更改其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29476790/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com