简体   繁体   English

如何将值传递给Angular JS服务

[英]How to pass values to Angular JS Service

I have implemented to Edit and Display values on text boxes etc but now want to transfer these code to Angular JS Service.Here is my code- 我已经实现了在文本框等上编辑和显示值的功能,但现在想将这些代码传输到Angular JS Service。这是我的代码-

app.factory('fetchEmpService', ['$rootScope', '$http', function ($rootScope, $http ) {
    var employees = [];
    return {

        editEmp: function (EID) {
            debugger;
            for (i in $rootScope.employees) {
                if ($rootScope.employees[i].EmpId == EID) {
                    $rootScope.newemployee = {
                        EmpId: $rootScope.employees[i].EmpId,
                        Name: $rootScope.employees[i].Name,
                        Age: $rootScope.employees[i].Age,
                        City: $rootScope.employees[i].City,
                        Gender: $rootScope.employees[i].Gender
                    };
                }
            }
        },


    };
}]);

I am using $rootScope instead of $scope (previously used in controller) object. 我正在使用$ rootScope而不是$ scope(以前在控制器中使用)对象。 Is it correct way because its returning nothing. 这是正确的方法,因为它什么也不返回。

It's not good practice to store data in $rootScope , you're correct in thinking you need a factory though. 将数据存储在$rootScope不是一个好习惯,尽管您认为自己需要factory是正确的。

Rather than storing the employees on $rootScope you can store them in the service. 与其将员工存储在$rootScope上,不如将他们存储在服务中。

app.factory('fetchEmpService', ['$http', function ($http) {
    var employees = [];

    return {
        getEmployees: function () {
            return employees;
        },
        setEmployees: function (newEmployees) {
            employees = newEmployees;
        },
        editEmp: function (EID) {
            for (var i in employees) {
                if (employees[i].EmpId == EID) {
                    return {
                        EmpId: employees[i].EmpId,
                        Name: employees[i].Name,
                        Age: employees[i].Age,
                        City: employees[i].City,
                        Gender: employees[i].Gender
                    };
                }
            }
        }
    };
}]);

Wherever you initially set $rootScope.employees , change it to fetchEmpService.setEmployees(foo); 无论您最初在何处设置$rootScope.employees ,都将其更改为fetchEmpService.setEmployees(foo); . To get your employees, fetchEmpService.getEmployees(); 要获取您的员工,请fetchEmpService.getEmployees(); .

I'm not sure how your editEmp function works so that's up to you to configure, but this is a sensible enough start. 我不确定您的editEmp函数如何工作,这取决于您进行配置,但这是一个明智的起点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular js如何传递数据以删除服务? - Angular js how to pass data to delete service? 如何将$ scope传递给angular.js中的服务? - How can I pass $scope to a service in angular.js? 如何将数据从服务传递到angular.js中的指令 - how to pass data from service to directive in angular.js 如何将url作为参数传递给Angular js服务中的ajax方法 - How to pass url as parameter to ajax method that is in a service in Angular js 如何将参数从angular 2服务传递给node js函数? 意思 - How to pass a parameter from angular 2 service to node js function? MEAN 如何将数据从Service Worker传递到Angular JS控制器 - How to pass data from service worker to Angular JS controller 如何将大量参数从Angular js传递到Rest服务 - How to pass large number of parameters from Angular js to rest service 如何在Angular JS中将$ rootScope变量从控制器传递到服务 - How to Pass $rootScope variable from Controller to Service in Angular JS Angular JS:如何将响应从服务传递到控制器? - Angular JS : How to pass the Response from a service to controller? 如何传递参数并从angular js中的自定义指令获取值? - how to pass parameter and get values from custom directive in angular js?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM