gpt4 book ai didi

javascript - $injector 在使用 Angular 继承时不工作

转载 作者:行者123 更新时间:2023-11-30 12:40:40 24 4
gpt4 key购买 nike

我正在学习 AngularJS 中的继承,所以我创建了一个示例来深入研究它。它从上到下按预期传达信息。但从 child 到 parent 不是。以下是tutorial我关注的是使用 $injector 。当我在我的代码中使用相同的代码时,它会中断,让我知道我在这里做错了什么。

我的整个代码-

<!doctype html>
<html lang="en" ng-app="myApp">
<head>
<title>Angular Inheritance</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.10/angular.min.js"></script>
<style>
html, body { text-align: center;}
div { border: 1px solid red; margin: 2px; padding: 2px;}
div div { border: 1px solid green; margin: 2px; padding: 2px;}
div div div { border: 1px solid blue; margin: 2px; padding: 2px;}
</style>
</head>
<body>
<div ng-controller="frstCtrl">
<p>I am <b>{{grandfather}}</b></p>
<p><b>{{father}}</b> is my son.</p>
<p><b>{{son}}</b> is my grandson.</p>
<div ng-controller="secCtrl">
<p>I am <b>{{father}}</b></p>
<p><b>{{grandfather}}</b> is my father.</p>
<p>{{son}} is my son</p>
<div ng-controller="thrdCtrl">
<p>I am <b>{{son}}</b></p>
<p><b>{{father}}</b> is my father.</p>
<p><b>{{grandfather}}</b> is my grandfather.</p>
</div>
</div>
</div>
</body>
</html>
<script>
var myApp = angular.module("myApp", []);

myApp.controller('frstCtrl', ['$scope', '$injector', function($scope, $injector){
$injector.invoke(thrdCtrl, this, {$scope: $scope});
$injector.invoke(secCtrl, this, {$scope: $scope});
$scope.grandfather = "Test GrandFather";
}]);

myApp.controller('secCtrl', ['$scope', '$injector', function($scope, $injector){
$injector.invoke(thrdCtrl, this, {$scope: $scope});
$scope.father = "Test Father";
}]);

myApp.controller('thrdCtrl', ['$scope', function($scope){
$scope.son = "Test Son";
}]);
</script>

最佳答案

你没有说它是怎么坏的,但我假设你在使用 secCtrlthrdCtrl 时遇到 ReferenceError?

如果您查看教程中的示例,您会发现它们定义了一个名为 CarController 的函数,然后在 BMWController 中引用该函数(通过闭包):

function CarController($scope) {
//...
}

function BMWController($scope, $injector) {
$injector.invoke(CarController, this, {$scope: $scope});
$scope.name = 'BMW';
}

您需要在您的示例中执行类似的操作才能使其正常工作。

例如,将函数 Controller 定义为命名函数,然后在 Angular 中设置它们应该可行:

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

myApp.controller('frstCtrl', ['$scope', '$injector', frstCtrl]);
function frstCtrl($scope, $injector){
$injector.invoke(thrdCtrl, this, {$scope: $scope});
$injector.invoke(secCtrl, this, {$scope: $scope});
$scope.grandfather = "Test GrandFather";
}

myApp.controller('secCtrl', ['$scope', '$injector', secCtrl]);
function secCtrl($scope, $injector){
$injector.invoke(thrdCtrl, this, {$scope: $scope});
$scope.father = "Test Father";
}

myApp.controller('thrdCtrl', ['$scope', thrdCtrl]);
function thrdCtrl($scope){
$scope.son = "Test Son";
}

如果您不清楚如何在它们“之前”引用命名函数(frstCtrlsrcCtrlthrdCtrl)重新声明看看这篇关于变量和函数提升的博文:http://designpepper.com/blog/drips/variable-and-function-hoisting

关于javascript - $injector 在使用 Angular 继承时不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24566890/

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