简体   繁体   English

如何从服务中删除$ scope对象

[英]How can I delete $scope object from service

Is it possible to delete $scope.contacts object from the service() function ? 是否可以从service()函数中删除$scope.contacts对象?

 var module = angular.module('app', []); module.service('ContactService', function() { //contacts array to hold list of all contacts this.delete = function(item) { console.log(item); var confirmDelete = confirm("Do you really need to delete " + item.name + " ?"); if (confirmDelete) { var curIndex = $scope.contacts.indexOf(item); $scope.contacts.splice(curIndex, 1); } } }); module.controller('ContactController', function($scope, ContactService) { $scope.contacts = [{ id: 0, 'name': 'Viral', 'email': 'hello@gmail.com', 'phone': '123-2343-44' }]; $scope.delete = function(id) { ContactService.delete(id); if ($scope.newcontact.id == id) $scope.newcontact = {}; } }) 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="ContactController" class="container"> <table class="table table-striped table-bordered"> <thead> <tr> <th>Name</th> <th>Email</th> <th>Phone</th> <th>Action</th> </tr> </thead> <tbody> <tr ng-repeat="contact in contacts"> <td>{{ contact.name }}</td> <td>{{ contact.email }}</td> <td>{{ contact.phone }}</td> <td> <a href="javascript:void(0)" ng-click="edit(contact.id)">edit</a> | <a href="javascript:void(0)" ng-click="delete(contact)">delete</a> </td> </tr> </tbody> </table> </div> 

This would not make sense, as a service is a singleton (one exists per app), whereas you can have multiple controllers. 这没有意义,因为服务是单例(每个应用程序一个),而您可以有多个控制器。 If the service could get hold of a scope, how does it know it got the right one! 如果服务可以控制一个范围,它怎么知道它找到了正确的范围!

You could make this work by passing the $scope into the delete function, like 您可以通过将$scope传递给delete函数来完成这项工作,例如

this.delete = function($scope, item)

and

ContactService.delete($scope, id)

Short answer: No, you can't. 简短的回答:不,你不能。

Long answer: No, you can't, because a scopes are tied to controllers that in turn are tied to views, and services are view-independent (hence their name ;-)). 长答案:不,您不能,因为范围与控制器绑定,而控制器又与视图绑定,并且服务与视图无关(因此,它们的名称为-)。

So, to cut a long story short: It's not possible to access a scope from within a service, because the service is responsible for multiple views (and scopes). 因此,简而言之:无法从服务内部访问范围,因为该服务负责多个视图(和范围)。 You will never know which scope to use, because for a service there is no "ambient" or "current" scope. 您将永远不会知道要使用哪个范围,因为对于服务而言,没有“环境”或“当前”范围。

If you actually want to to access the scope directly, hand it over to the service (as suggested in the answer by Andy Newman ). 如果您确实想直接访问该范围,请将其移交给该服务(如Andy Newman答案中所建议)。 But I'd not recommend this, because then the service acts on something it should not know about. 但是我不建议这样做,因为这样服务就会对它不应该知道的东西起作用。

A better approach would be to think about messaging, so that the service lets all interested parties (ie, controllers) know to do something on their scope. 更好的方法是考虑消息传递,以便该服务让所有感兴趣的方(即控制器)知道在其范围内做某事。

Probably the best option is not to hold the data in the controller, but to put the data into the service, so that it's the service's responsibility to deal with the data and delete them when appropriate. 最好的选择可能不是将数据保存在控制器中,而是将数据放入服务中,因此处理数据并在适当时删除它们是服务的责任。 Controllers then only need to tell the service what to do. 然后,控制器仅需要告诉服务该怎么做。

You don't have to send the $scope. 您不必发送$ scope。 Just send the contacts array: 只需发送联系人数组:

 var module = angular.module('app', []); module.service('ContactService', function () { //contacts array to hold list of all contacts this.delete = function (contacts, item) { var confirmDelete = confirm("Do you really need to delete " + item.name + " ?"); if (!confirmDelete) { return; } var curIndex = contacts.indexOf(item); contacts.splice(curIndex, 1); }; }); module.controller('ContactController', function ($scope, ContactService) { $scope.contacts = [{ id : 0, 'name' : 'Viral', 'email' : 'hello@gmail.com', 'phone' : '123-2343-44' } ]; $scope.delete = function (item) { var id = item.id; ContactService.delete ($scope.contacts, item); if ($scope.newcontact.id == id) $scope.newcontact = {}; } }); 
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="ContactController" class="container"> <table class="table table-striped table-bordered"> <thead> <tr> <th>Name</th> <th>Email</th> <th>Phone</th> <th>Action</th> </tr> </thead> <tbody> <tr ng-repeat="contact in contacts"> <td>{{ contact.name }}</td> <td>{{ contact.email }}</td> <td>{{ contact.phone }}</td> <td> <a href="javascript:void(0)" ng-click="edit(contact.id)">edit</a> | <a href="javascript:void(0)" ng-click="delete(contact)">delete</a> </td> </tr> </tbody> </table> </div> 

I think adding $scope to a service is not the Angular way. 我认为向服务添加$scope不是Angular的方式。

It's better to let the service manage your data (as mentioned in Golo's answer). 最好让服务管理您的数据(如Golo的答案所述)。 Then you can also do $http request to keep your model/data in synch. 然后,您也可以执行$http请求以保持模型/数据同步。 with your backend. 与您的后端。

Have a look at the demo below or in this fiddle . 看看下面或这个小提琴中的演示。

 angular.module('app', []) .factory('contactService', function () { //contacts array to hold list of all contacts var service = { contacts: [{ id: 0, 'name': 'Viral', 'email': 'hello@gmail.com', 'phone': '123-2343-44' }, { id: 1, 'name': 'John', 'email': 'hello@gmail.com', 'phone': '123-2343-44' }, { id: 2, 'name': 'Jane', 'email': 'hello@gmail.com', 'phone': '123-2343-44' }], delete: function (item) { console.log(item); var itemIndex; var confirmDelete = confirm("Do you really need to delete " + item.name + " ?"); if (confirmDelete) { angular.forEach(this.contacts, function(contact, index) { if(item.id === contact.id) { itemIndex = index; } }); this.contacts.splice(itemIndex, 1); } } }; return service; }) .controller('contactController', function ($scope, contactService) { $scope.contacts = contactService.contacts; $scope.delete = function (contact) { contactService.delete(contact); //if ($scope.newcontact.id == id) $scope.newcontact = {}; } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="contactController"> <ul> <li ng-repeat="contact in contacts">{{contact.name}} <button ng-click="delete(contact)">remove</button> </li> </ul> </div> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM