在猫鼬插入数据使用工厂 [英] Insert data in mongoose using a factory
本文介绍了在猫鼬插入数据使用工厂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图插入使用angularjs和节点JS在猫鼬的数据。
为了这个,我已经创建了一个工厂,我打电话在那里我有我的创建数据库连接另一个js文件。
但是,当我试图做到这一点,它给我的错误。
下面是我厂的方法:
使用严格
test.factory('registrationservice',函数($ HTTP,$范围){
的console.log('AA');
$ scope.newregister =功能(用户,$范围){ VAR NEWUSER =新用户({
用户名:$ scope.uName,
姓:$ scope.fName,
名字:$ scope.lName,
电子邮件:$ scope.mail,
密码:$ scope.newpwd }); console.dir(NEWUSER); 变量$承诺= $ http.post(数据/ registration.js',NEWUSER);
$ promise.then(函数(MSG){
如果(msg.data =='成功')的console.log('成功登陆')
其他
的console.log('登录失败');
});
};});
和下面是我收到的错误:
错误:[$喷油器:unpr] http://errors.angularjs.org/1.2.7/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20registrationservice
在错误(原生)
解决方案
您不能注入$范围工厂。服务没有范围。只有控制器。
所以,你必须使用这样的事情。
test.factory('registrationservice',函数($ HTTP){
变种工厂= {};
//如果用户是另一种服务,你必须在工厂defenotion功能,它注入
//从这里删除。
facotry.newregister =功能(用户,$范围){ VAR NEWUSER =新用户({
用户名:$ scope.uName,
姓:$ scope.fName,
名字:$ scope.lName,
电子邮件:$ scope.mail,
密码:$ scope.newpwd }); 返回$ http.post(数据/ registration.js',NEWUSER);
}
回厂;
});
然后在你的控制器。
test.controller('registrationCtrl',函数($范围,$日志,registrationservice){
registrationservice.newregister(用户,$范围).success(函数(MSG){
$ log.info(msg.data);
})
});
I am trying to insert data in mongoose using angularjs and node js. For this i have created a factory where i am calling another js file where i have created my database connection.
But when i tried to do this, it is giving me error.
Here is my factory method:
'use strict'
test.factory('registrationservice', function($http, $scope){
console.log('aa');
$scope.newregister = function(user, $scope) {
var newUser = new user({
username: $scope.uName,
firstname: $scope.fName,
lastname: $scope.lName,
email:$scope.mail,
password: $scope.newpwd
});
console.dir(newUser);
var $promise = $http.post('data/registration.js', newUser);
$promise.then(function(msg){
if(msg.data == 'success') console.log('success login')
else
console.log('login failed');
});
};
});
and below is the error i am getting:
Error: [$injector:unpr] http://errors.angularjs.org/1.2.7/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20registrationservice
at Error (native)
解决方案
You cannot inject $scope to factory. Services do not have scopes. Only Controllers.
So you have to use something like this.
test.factory('registrationservice', function($http){
var factory = {};
// if user is another service you have to inject it in factory defenotion function
// and delete from here.
facotry.newregister = function(user, $scope) {
var newUser = new user({
username: $scope.uName,
firstname: $scope.fName,
lastname: $scope.lName,
email:$scope.mail,
password: $scope.newpwd
});
return $http.post('data/registration.js', newUser);
}
return factory;
});
And then in your controller.
test.controller('registrationCtrl', function($scope, $log, registrationservice){
registrationservice.newregister(user, $scope).success(function(msg){
$log.info(msg.data);
})
});
这篇关于在猫鼬插入数据使用工厂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文