Javascript angularJS代码中没有发生继承
这是一个AngularJS继承代码,继承应用于函数中,但此代码没有输出。Javascript angularJS代码中没有发生继承,javascript,angularjs,Javascript,Angularjs,这是一个AngularJS继承代码,继承应用于函数中,但此代码没有输出。 custDetails和empPaycheck是应用继承的两个函数,但代码中有一些错误,我无法找到 <html lang=""> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
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>
控制器继承
var app=angular.module(“示例”,
[])。运行([“$rootScope”,函数($rootScope){
$rootScope.taxPe`在此处输入代码`rcent=30;
}]);
应用程序控制器(“custDetails”、[“$scope”、函数($scope){
$scope.Name=“Dipanshu”;
$scope.Sal=45000;
$scope.Dept=“测试”;
}]);
app.controller(“empPayCheck”[“$scope”,“$rootScope”,
函数($scope,$rootScope){
$scope.getTaxes=函数(){
返回$scope.Sal*$rootScope.taxPercent/100;
};
$scope.getNet=function(){
返回$scope.Sal-$scope.getTaxes();
};
}]);
{{Name}的员工详细信息
{{Name}赚取{{Sal}卢比,并且在{{Dept}}
部门
税:{{getTaxes()}}
净额:{{getNet()}}
$rootScope.taxPe附近的模块运行块中有错误李>
您使用的是controller
属性,而不是
下面是一个工作代码段:
var-app=angular.module(“示例”,[])。运行([“$rootScope”,函数($rootScope){
$rootScope.taxPercent=30;
}]);
应用程序控制器(“custDetails”、[“$scope”、函数($scope){
$scope.Name=“Dipanshu”;
$scope.Sal=45000;
$scope.Dept=“测试”;
}]);
app.controller(“empPayCheck”[“$scope”,“$rootScope”,
函数($scope,$rootScope){
$scope.getTaxes=函数(){
返回$scope.Sal*$rootScope.taxPercent/100;
};
$scope.getNet=function(){
返回$scope.Sal-$scope.getTaxes();
};
}
]);代码>
{{Name}的员工详细信息
{{Name}赚取{{Sal}卢比,并在{{Dept}部门工作。
税:{{getTaxes()}}
净额:{{getNet()}}
您应该使用$scope.$parent
访问子控制器中的父$scope
变量。
在HTML代码中还有一个输入错误,controller
应该是ng controller
请看下面的工作示例
var-app=angular.module(“示例”,[])。运行([“$rootScope”,函数($rootScope){
$rootScope.taxPercent=30;
}]);
应用程序控制器(“custDetails”、[“$scope”、函数($scope){
$scope.Name=“Dipanshu”;
$scope.Sal=45000;
$scope.Dept=“测试”;
}]);
app.controller(“empPayCheck”[“$scope”,“$rootScope”,
函数($scope,$rootScope){
$scope.getTaxes=函数(){
返回$scope.$parent.Sal*$rootScope.taxPercent/100;
};
$scope.getNet=function(){
返回$scope.$parent.Sal-$scope.getTaxes();
};
}
]);代码>
{{Name}的员工详细信息
{{Name}赚取{{Sal}卢比,并在{{Dept}部门工作。
税:{{getTaxes()}}
净额:{{getNet()}}
非常感谢,伙计@Thusitha@Dipanshu很高兴帮助你。您可以将其标记为正确答案;)