[英]Angular JS push data from factory into controller
I have a simple page with an input field and a button to change the name. 我有一个简单的页面,其中包含一个输入字段和一个用于更改名称的按钮。 The name changes on ng-click of the button, but what I want to happen is below the hello {{name}} text to have a log of the previous names selected and the time they were selected.
名称会在ng-click按钮上发生变化,但是我想发生的是在{{name}}文本下方,以记录先前选定的名称和选定的时间。 eg
例如
Hello Tom 你好汤姆
I have tried the below in this fiddle http://jsfiddle.net/4ybmyf1a/2/ but get the message, 'cannot read property push of undefined' (I have commented out in myCtrl so that the fiddle would run) 我已经在这个小提琴中尝试了以下内容http://jsfiddle.net/4ybmyf1a/2/,但收到消息“无法读取未定义的属性推送”(我在myCtrl中已注释掉,以便小提琴可以运行)
<div ng-controller="MyCtrl">
<input type="text" ng-model="updatedname" />
<input type="button" value="Change name" ng-click="changeName(updatedname)"/>
<br/>
Hello, {{name}}!
<ul>
<li ng-repeat="names in namelog">{{nameLog.value}} - {{nameLog.time}}</li>
</ul>
</div>
var myApp = angular.module('myApp',[]);
myApp.factory('UserService', function() {
var userService = {};
userService.name = "John";
userService.ChangeName = function (value) {
userService.name = value;
};
userService.NameLog = function (value) {
userService.nameLog.push ({
"value":value,
"time" :Date.now()
});
};
return userService;
});
function MyCtrl($scope, UserService) {
$scope.name = UserService.name;
$scope.updatedname="";
$scope.changeName=function(data){
$scope.updateServiceName(data);
}
$scope.updateServiceName = function(name){
UserService.ChangeName(name);
//UserService.NameLog(name);
$scope.name = UserService.name;
//$scope.nameLog = UserService.NameLog;
}
}
I have looked at also adding in userService.nameLog = [] to stop the undefined issue however this does not push the items like I want it. 我也查看了是否添加userService.nameLog = []以停止未定义的问题,但是这并没有像我想要的那样推送项目。
userService.NameLog = function (value) {
userService.nameLog = [];
userService.nameLog.push ({
"value":value,
"time" :Date.now()
});
};
How am I able to acheive this? 我怎样才能做到这一点?
This is your first problem: 这是您的第一个问题:
<li ng-repeat="names in namelog">{{nameLog.value}} - {{nameLog.time}}</li>
the variable you should be accessing in this ng-repeat
is names
, so this is the correct form (I also renamed the variable to make more sense): 您应该在此
ng-repeat
访问的变量是names
,所以这是正确的形式(我也将变量重命名为更有意义):
<li ng-repeat="logEntry in namelog">{{logEntry.value}} - {{logEntry.time}}</li>
And second problem is your controller, here is a correct version: 第二个问题是您的控制器,这是一个正确的版本:
function MyCtrl($scope, UserService) {
$scope.name = UserService.name;
// Add the name log to the scope here, not when you
// invoke the updateServiceName function
$scope.namelog = UserService.nameLog;
$scope.changeName = function(data){
$scope.updateServiceName(data);
}
$scope.updateServiceName = function(name){
UserService.ChangeName(name);
UserService.NameLog(name);
$scope.name = UserService.name;
}
}
In your version, you have commented out the following code: 在您的版本中,您已注释掉以下代码:
//$scope.nameLog = UserService.NameLog;
That is wrong for two reasons. 这有两个原因。 First the case of the nameLog variable is different in the controller and in the template (nameLog vs namelog).
首先,控制器和模板中nameLog变量的情况不同(nameLog与namelog)。 Second your are assigning it reference to the
NameLog
function not the nameLog
property (again, notice the case). 其次,您将其分配给
NameLog
函数引用,而不是nameLog
属性(再次注意这种情况)。
When I made these modifications in your Fiddle it started working correctly. 当我在您的Fiddle中进行了这些修改后,它便开始正常工作。
And by the way, I think that the logic for adding log entry when you change username should be in the service itself not in the controller function. 顺便说一句,我认为在更改用户名时添加日志条目的逻辑应该在服务本身中,而不是在控制器功能中。
Couple different things. 夫妇不同的事情。
Your factory function LogName was referencing itself when pushing the log entry. 推送日志条目时,工厂函数LogName正在引用自身。 This is the sort of thing you want to declare outside of the service object (
_nameLog
) and use a closure to return it back ( getNameLog
) 您需要在服务对象(
_nameLog
)之外声明该类型,并使用闭包将其返回( getNameLog
)
myApp.factory('UserService', function() {
var _nameLog = [];
var userService = {};
userService.name = "John";
userService.ChangeName = function (value) {
userService.name = value;
};
userService.logName = function (value) {
_nameLog.push ({
"value":value,
"time" :Date.now()
});
};
userService.getNameLog = function(){ return _nameLog; }
return userService;
});
You were also using the ng-repeat incorrectly: 您还错误地使用了ng-repeat:
<li ng-repeat="name in nameLog">{{name.value}} - {{name.time}}</li>
Here is a working version: http://jsfiddle.net/sfqo2fn3/ 这是一个工作版本: http : //jsfiddle.net/sfqo2fn3/
I updated the jsfiddle with a working version after changing few things. 更改了几件事后,我用工作版本更新了jsfiddle。 Please refer it.
请参考。
http://jsfiddle.net/4ybmyf1a/4/ http://jsfiddle.net/4ybmyf1a/4/
These are the changes 这些是变化
<li ng-repeat="names in nameLog">{{names.value}} - {{names.time}}</li>
myApp.factory('UserService', function() {
var userService = {};
var nameLog = [];
userService.name = "John";
userService.ChangeName = function (value) {
userService.name = value;
};
userService.addNameToLog = function (value) {
nameLog.push ({
"value":value,
"time" :Date.now()
});
return nameLog;
};
userService.getNameLog = function() {
return nameLog;
}
return userService;
});
Controller 调节器
function MyCtrl($scope, UserService) {
$scope.name = UserService.name;
$scope.updatedname="";
$scope.changeName = function(data){
$scope.updateServiceName(data);
}
$scope.updateServiceName = function(name){
UserService.ChangeName(name);
UserService.addNameToLog(name);
$scope.name = UserService.name;
$scope.nameLog = UserService.getNameLog();
console.log(JSON.stringify($scope.nameLog));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.