This question already has an answer here:
这个问题在这里已有答案:
- Passing data between controllers in Angular JS? 18 answers
在Angular JS中的控制器之间传递数据? 18个答案
I have seen various post on stack-overflow and I try to flow, but some mistake I am doing while creating services below is my code kindly help me to find out where I am doing wrong
我已经看到了关于堆栈溢出的各种帖子,我尝试流动,但是我在创建服务时正在做的一些错误是我的代码请帮助我找出我做错的地方
angular
.module('yolomd', ['ngSanitize']);
app.factory('myplacementdata', function () {
var mydata = {
placementdata = [],
getplacementdataValue:function () {
return mydata.placementdata;
},
setplacementdataValue: function (data) {
mydata.placementdata = data;
}
}
return mydata;
});
app.controller('PriorityPlacement',['$scope', '$rootScope','myplacementdata', function($scope, $rootScope,myplacementdata){
var response.Data=[{city_name: "Toronto", city_placement_charge: "1799"}, yolomd_verify: "50"];
myplacementdata.setplacementdataValue=response.Data;
console.log(myplacementdata.getplacementdataValue);
//window.location = site_url + 'Overview'
}]);
app.controller('Overview',['$scope', '$rootScope','$stateParams','myplacementdata', function($scope, $rootScope,myplacementdata){
console.log(myplacementdata.getplacementdataValue);
}])
<html lang="en" ng-app="yolomd">
<head>
<script src="http://205.134.251.196/~examin8/CI/yoloMD/assets/front/js/vendor/angular.min.js"></script>
</head>
2 个解决方案
#1
1
Please note that :
请注意:
setplacementdataValue()
getplacementdataValue()
These are methods and not variables.
这些是方法而不是变量。
So use them as :
所以使用它们:
setplacementdataValue([Your Array Input]);
console.log(getplacementdataValue());
Note the parenthesis.
注意括号。
Also in your set method, use a different input parameter name.
同样在set方法中,使用不同的输入参数名称。
var app = angular.module("myApp", []);
app.controller('PriorityPlacement', ['myplacementdata',
function(myplacementdata) {
myplacementdata.setplacementdataValue([1, 2, 3]);
console.log(myplacementdata.getplacementdataValue());
}
]);
app.controller('Overview', ['myplacementdata',
function(myplacementdata) {
console.log(myplacementdata.getplacementdataValue());
}
]);
app.service('myplacementdata', function() {
var placementdata = [];
this.getplacementdataValue = function() {
return placementdata;
}
this.setplacementdataValue = function(myplacementdata) {
placementdata = myplacementdata;
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="PriorityPlacement">
</div>
<div ng-controller="Overview">
</div>
</div>
#2
1
First off try changing
首先尝试改变
myplacementdata.setplacementdataValue=response.Data;
To
myplacementdata.setplacementdataValue(response.Data);
And where do u get the response.Data from?
你从哪里得到response.Data来自?
try using a factory instead. like so.
尝试使用工厂代替。像这样。
app.factory('myplacementdata', function () {
var mydata = {
placementdata = [],
getplacementdataValue:function () {
return mydata.placementdata;
},
setplacementdataValue: function (data) {
mydata.placementdata = data;
}
}
return mydata;
});
#1
1
Please note that :
请注意:
setplacementdataValue()
getplacementdataValue()
These are methods and not variables.
这些是方法而不是变量。
So use them as :
所以使用它们:
setplacementdataValue([Your Array Input]);
console.log(getplacementdataValue());
Note the parenthesis.
注意括号。
Also in your set method, use a different input parameter name.
同样在set方法中,使用不同的输入参数名称。
var app = angular.module("myApp", []);
app.controller('PriorityPlacement', ['myplacementdata',
function(myplacementdata) {
myplacementdata.setplacementdataValue([1, 2, 3]);
console.log(myplacementdata.getplacementdataValue());
}
]);
app.controller('Overview', ['myplacementdata',
function(myplacementdata) {
console.log(myplacementdata.getplacementdataValue());
}
]);
app.service('myplacementdata', function() {
var placementdata = [];
this.getplacementdataValue = function() {
return placementdata;
}
this.setplacementdataValue = function(myplacementdata) {
placementdata = myplacementdata;
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="PriorityPlacement">
</div>
<div ng-controller="Overview">
</div>
</div>
#2
1
First off try changing
首先尝试改变
myplacementdata.setplacementdataValue=response.Data;
To
myplacementdata.setplacementdataValue(response.Data);
And where do u get the response.Data from?
你从哪里得到response.Data来自?
try using a factory instead. like so.
尝试使用工厂代替。像这样。
app.factory('myplacementdata', function () {
var mydata = {
placementdata = [],
getplacementdataValue:function () {
return mydata.placementdata;
},
setplacementdataValue: function (data) {
mydata.placementdata = data;
}
}
return mydata;
});