gpt4 book ai didi

javascript - 获取 Function.prototype.bind.apply(...) 不是构造函数错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:06:46 25 4
gpt4 key购买 nike

我正在尝试在 AngularJS (1.6.9) 中模拟 Controller 继承,但我在控制台上收到错误消息:Function.prototype.bind.apply(...) is not a constructor
这是 HTML 文件:

<!-- Controller Inheritance -->

<!DOCTYPE html>
<html lang="en" ng-app="app7">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Tutorial 7</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
</head>
<body>
<div ng-controller="mainCtrl as parent">
<p>Name: {{parent.name}}</p>
<p>Sound: {{parent.sound}}</p>
<button ng-click="parent.animalClick()">Animal Data</button>
</div>
<br><br>
<div ng-controller="dogCtrl as dog">
<p>Name: {{dog.child.name}}</p>
<p>Sound: {{dog.child.sound}}</p>
<button ng-click="dog.child.animalClick()">Dog Data</button>
<button ng-click="dog.child.dogData()">Get More Data</button>
</div>
<script src="js/exam7.js"></script>
</body>
</html>

这是 JS 文件:

//Controller Inheritance Demonstration

let app7 = angular.module('app7',[]);

//Parent Controller

app7.controller('mainCtrl',()=>{
this.name="Animal";
this.sound="Silent";

this.animalClick= ()=>{
alert(this.name+' says '+this.sound);
};
});

//Child Controller

app7.controller('dogCtrl',($controller)=>{
let childCtrl = this;
childCtrl.child=$controller('mainCtrl',{});
childCtrl.child.name="Dog";
childCtrl.child.bark="Woof"; //child`s own variable

childCtrl.child.dogData = ()=>{
alert(this.name+' says '+this.sound+' and '+this.bark);
};
});

我试图在 childCtrl 中继承 mainCtrl 但无法这样做。输出不符合预期。这种错误背后的可能原因是什么?

最佳答案

你不能在 AngularJS 中到处使用arrow notation

AngularJS 尝试使用 new your_function(){...} 方法调用一个函数,将其视为一个类,但它无法使用箭头符号 new () =>{...}.

简单的改变

app7.controller('mainCtrl',()=>{

app7.controller('mainCtrl',function(){

(以及其他地方)


您在打印子值时也有错误的逻辑。在打印任何内容之前,您需要先访问 .child. 子属性。

这是您的代码的一个工作示例:

let app7 = angular.module('app7', []);

//Parent Controller

app7.controller('mainCtrl', function() {
this.name = "Animal";
this.sound = "Silent";

this.animalClick = () => {
alert(this.name + ' says ' + this.sound);
};
});

//Child Controller

app7.controller('dogCtrl', function($controller) {
let childCtrl = this;
childCtrl.child = $controller('mainCtrl', {});
childCtrl.child.name = "Dog";
childCtrl.child.bark = "Woof"; //child`s own variable

childCtrl.child.dogData = () => {
alert(this.child.name + ' says ' + this.child.sound + ' and ' + this.child.bark);
};
});
<!DOCTYPE html>
<html lang="en" ng-app="app7">

<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Tutorial 7</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
</head>

<body>
<div ng-controller="mainCtrl as parent">
<p>Name: {{parent.name}}</p>
<p>Sound: {{parent.sound}}</p>
<button ng-click="parent.animalClick()">Animal Data</button>
</div>
<br><br>
<div ng-controller="dogCtrl as dog">
<p>Name: {{dog.child.name}}</p>
<p>Sound: {{dog.child.sound}}</p>
<button ng-click="dog.child.animalClick()">Dog Data</button>
<button ng-click="dog.child.dogData()">Get More Data</button>
</div>

</body>

</html>

关于javascript - 获取 Function.prototype.bind.apply(...) 不是构造函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49273401/

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