gpt4 book ai didi

javascript - 使用示例服务在 Controller 之间传递数据时出错

转载 作者:行者123 更新时间:2023-11-30 11:53:56 25 4
gpt4 key购买 nike

作为 angular js 学习的一部分,我创建了一个小应用程序,它将使用服务在两个 Controller 之间传递数据。下面是我在两个 Controller 之间传递数据的代码。

Controller 代码

<!DOCTYPE html>
<html lang="en-us" ng-app="myApp">
<head>
<title></title>
<script src="https://code.angularjs.org/1.5.8/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('myApp', []);

app.service('sampleService', [function () {
this.user = [name = 'pra', empno ='1234']
this.designation = 'teamlead';

}])
app.controller('NameCtrl', ['$scope','$sampleService', function ($scope,$sampleService) {
$scope.empname = $sampleService.user.name;
$scope.empnumber = $sampleService.user.empno;
$scope.empdesignation = $sampleService.designation;
$scope.changevals = function(){
$sampleService.user.empno = '9876';
$sampleService.designation = 'manager';
$scope.empnumber = $sampleService.user.empno;
$scope.empdesignation = $sampleService.designation; }
}])

app.controller('NameCtrl1', ['$scope','$sampleService', function ($scope) {
$scope.uEmpiId = $sampleService.user.empno;
$scope.uempdesignation = $sampleService.designation;
$scope.updatevals = function(){
$scope.uEmpiId = $sampleService.user.empno;
$scope.uempdesignation = $sampleService.designation; }
}])
</script>
</head>

下面的代码是我的html代码

<body>
<div ng-controller="NameCtrl">
<div><b> Details - Controller 1</b></div>
<p>Name : {{empname}}</p>
<p>Location : {{empnumber}}</p>
<p>Designation : {{empdesignation}}</p>
<input type="button" value="Change Values" ng-click="changevals()" />
</div>
<br />
<div ng-controller="NameCtrl1">
<div><b>Details - Controller 2</b></div>
<input type="button" value="Change Values" ng-click="updatevals()" />
<p>Location : {{uEmpiId}}</p>
<p>Designation : {{uempdesignation}}</p>
</div>
</body>

我无法显示详细信息并收到这样的错误

angular.js:13920 错误:[$injector:unpr] http://errors.angularjs.org/1.5.8/ $injector/unpr?p0=%24sampleServiceProvider%20%3C-%20%24sampleService%20%3C-%20NameCtrl 错误时( native )

任何人都可以指出我在这段代码中做错的正确方向..

提前致谢....

最佳答案

在 Controller 中注入(inject)服务时不要使用“$”:

改变:

app.controller('NameCtrl', ['$scope','$sampleService', function ($scope,$sampleService) {

到:

app.controller('NameCtrl', ['$scope','sampleService', function ($scope,sampleService) {

编辑

另外(正如@estus 所建议的),您的服务在 NameCtrl1

中未定义

更改您的 NameCtrl2 的代码

app.controller('NameCtrl1', ['$scope','$sampleService', function ($scope) {

app.controller('NameCtrl1', ['$scope','sampleService', function ($scope, sampleService) {

在您的代码中,无论何时使用此 sampleService,都不要使用 sampleService 而不是 $sampleService


编辑 2

改变你的

app.service('sampleService', [function () {
this.user = [name = 'pra', empno ='1234']
this.designation = 'teamlead';
}])

app.service('sampleService', [function() {
this.user = {name : 'pra', empno : '1234'};
this.designation = 'teamlead';
}])

最终代码如下所示:

<!DOCTYPE html>
<html lang="en-us" ng-app="myApp">

<head>
<title></title>
<script src="https://code.angularjs.org/1.5.8/angular.min.js"></script>
<script type="text/javascript">
var app = angular.module('myApp', []);

app.service('sampleService', [function() {
this.user = {name : 'pra', empno : '1234'};
this.designation = 'teamlead';

}])
app.controller('NameCtrl', ['$scope', 'sampleService', function($scope, sampleService) {
$scope.empname = sampleService.user.name;
$scope.empnumber = sampleService.user.empno;
$scope.empdesignation = sampleService.designation;
$scope.changevals = function() {
sampleService.user.empno = '9876';
sampleService.designation = 'manager';
$scope.empnumber = sampleService.user.empno;
$scope.empdesignation = sampleService.designation;
}
}])

app.controller('NameCtrl1', ['$scope', 'sampleService', function($scope, sampleService) {
$scope.uEmpiId = sampleService.user.empno;
$scope.uempdesignation = sampleService.designation;
$scope.updatevals = function() {
$scope.uEmpiId = sampleService.user.empno;
$scope.uempdesignation = sampleService.designation;
}
}])
</script>
</head>
<body>
<div ng-controller="NameCtrl">
<div><b> Details - Controller 1</b>
</div>
<p>Name : {{empname}}</p>
<p>Location : {{empnumber}}</p>
<p>Designation : {{empdesignation}}</p>
<input type="button" value="Change Values" ng-click="changevals()" />
</div>
<br />
<div ng-controller="NameCtrl1">
<div><b>Details - Controller 2</b>
</div>
<input type="button" value="Change Values" ng-click="updatevals()" />
<p>Location : {{uEmpiId}}</p>
<p>Designation : {{uempdesignation}}</p>
</div>
</body>
</html>

关于javascript - 使用示例服务在 Controller 之间传递数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38635868/

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