gpt4 book ai didi

jquery - 无法开始执行某个功能

转载 作者:行者123 更新时间:2023-12-01 07:48:19 27 4
gpt4 key购买 nike

我编写了一个相当简单的函数,其中包含一些 Jquery 代码,但由于某种原因我无法让它工作。

我错过了什么?

Codepen

HTML

<div ng-app='myApp'>
<div ng-controller="DropdownCtrl">

<p>Here should be five -> &nbsp;</p>

</div>
</div>

JS

var myApp = angular.module('myApp', []);

myApp.controller('MainCtrl', function ($scope) {

$scope.onload = function(scope) {
$('p').each(function(){
$(this).html($(this).html().replace(/&nbsp;/gi,"5"));
});
};

});

最佳答案

You shouldn't be manipulating the DOM in a controller有很多原因,Angular 文档和许多 StackOverflow 票证中对此进行了详细说明。

推荐阅读:

Angular Controller 中没有 load 事件(如果有,则会使用 $scope.$on('load',function(){}); 调用方法)。

您应该在此处使用 Angular 数据绑定(bind)/插值。这是一个例子:

<p>Here should be five -> {{number}}</p>

还有你的 Controller :

var myApp = angular.module('myApp', []);
myApp.controller('MainCtrl', function ($scope) {
$scope.number = 5;
});

关于jquery - 无法开始执行某个功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33271025/

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