gpt4 book ai didi

javascript - AngularJS 代码中没有发生继承

转载 作者:行者123 更新时间:2023-12-03 03:25:02 25 4
gpt4 key购买 nike

这是一个 AngularJS 继承代码,其中继承应用于函数,但此代码没有输出。
custDetails 和 empPaycheck 是应用继承的两个函数,但代码有一些我无法找到的错误。

 <html lang="">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Controller Inheritance</title>

<script src="angulaar.min.js"></script>
<script>
var app = angular.module("sample",
[]).run(["$rootScope",function($rootScope){
$rootScope.taxPe`enter code here`rcent = 30;
}]);

app.controller("custDetails", ["$scope",function($scope) {
$scope.Name = "Dipanshu";
$scope.Sal = 45000;
$scope.Dept = "Testing";
}]);

app.controller("empPayCheck", ["$scope", "$rootScope",
function($scope, $rootScope) {
$scope.getTaxes = function() {
return $scope.Sal * $rootScope.taxPercent / 100;
};

$scope.getNet = function() {
return $scope.Sal - $scope.getTaxes();
};
}]);
</script>

</head>

<body ng-app="sample">
<div ng-controller="custDetails">
Employee Details of {{Name}}

<div ng-controller="custDetails">
{{Name}} earns {{Sal}} rs and is in <strong>{{Dept}}</strong>
Department.

<div controller="empPayCheck">
Tax: {{getTaxes()}}
<br> Net Amount: {{getNet()}}
</div>
</div>
</div>
</body>

</html>

最佳答案

您应该使用 $scope.$parent 访问子 Controller 中的父 $scope 变量。另外,您的 HTML 代码中存在一个拼写错误,其中 controller 应为 ng-controller
请看下面的工作示例。

var app = angular.module("sample", []).run(["$rootScope", function($rootScope) {
$rootScope.taxPercent = 30;
}]);

app.controller("custDetails", ["$scope", function($scope) {
$scope.Name = "Dipanshu";
$scope.Sal = 45000;
$scope.Dept = "Testing";
}]);

app.controller("empPayCheck", ["$scope", "$rootScope",
function($scope, $rootScope) {

$scope.getTaxes = function() {
return $scope.$parent.Sal * $rootScope.taxPercent / 100;
};

$scope.getNet = function() {
return $scope.$parent.Sal - $scope.getTaxes();
};
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="sample">
<div ng-controller="custDetails">
Employee Details of {{Name}}

<div>
{{Name}} earns {{Sal}} rs and is in <strong>{{Dept}}</strong> Department.

<div ng-controller="empPayCheck">
Tax: {{getTaxes()}}
<br> Net Amount: {{getNet()}}
</div>
</div>
</div>
</body>

关于javascript - AngularJS 代码中没有发生继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46369540/

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