简体   繁体   English

角度分离,何时使用服务和/或工厂?

[英]Separation of concerns in angular, when to use a service and/or factory?

I've been getting to grips with Angular lately and trying to get my head round the concepts that it's built on. 我最近一直在与Angular交手并试图了解它所构建的概念。 All a little cryptic so far but I'm getting there! 到目前为止有点神秘,但我到了那里!

Anyway so I've built a small part of my web site using Angular (it seemed to fit the use case as I needed to manipulate a JavaScript object on the browser). 无论如何所以我使用Angular构建了我的网站的一小部分(它似乎适合用例,因为我需要在浏览器上操作JavaScript对象)。

As part of this process I needed to make add an object to my model (on click of an element). 作为这个过程的一部分,我需要在我的模型中添加一个对象(点击一个元素)。 It took me a while to figure out how to do this but eventually I got it working. 我花了一段时间才弄清楚如何做到这一点,但最终我得到了它的工作。

Now I ended up with the code below: 现在我最终得到了以下代码:

var stpApp = angular.module('stpApp', []);

stpApp.controller('multiStopController', function ($scope, $compile, $http) {
    $scope.inboundJourney = [{
        'DepartureAirport': '',
            'DestinationAirport': '',
            'DepartureDate': '',
            'DepartureTime': 9,
            'Class': 'Class'
    }];

    $scope.addInboundJourney = function () {
        $scope.inboundJourney.push({
            'DepartureAirport': '',
                'DestinationAirport': '',
                'DepartureDate': '',
                'DepartureTime': 9,
                'Class': ''
        });
    }
});

and some mark up: 一些标记:

<li ng-repeat="journey in inboundJourney">
    <input type="text" class="AirportName" ng-model="journey.DepartureAirport" />
</li>
<p class="addMultiStop"><span title="Add a journey" ng-click="addInboundJourney()">+</span>
</p>

Doing a little more reading I've seen the concepts of services and factories getting introduced. 再多做一些阅读我已经看到了servicesfactories的概念。 So should my addInboundJourney function be in a service or a factory, or is it ok in the controller, like it is now? 那么我的addInboundJourney函数应该在服务或工厂中,还是在控制器中可以,就像它现在一样?

If this is ok in the controller, when is it advised to use a service and/or factory? 如果控制器中没有问题,建议何时使用服务和/或工厂?

That's quite the subjective question. 这是一个非常主观的问题。 Move your inboundJourney manipulation into a service if you need to handle the manipulation in more than one controller, and inject it into the controllers. 如果需要在多个控制器中处理操作,并将其注入控制器,请将inboundJourney操作移动到服务中。

If you're only ever going to manipulate the inboundJourneys in the multiStopController, then I'd say you're good to go on as you were. 如果你只是想在multiStopController中操纵inboundJourneys,那么我会说你继续像往常一样好。

If however you find that there's alot of code for the inboundJourneys manipulation (you might want to do some other things in the multiStopController aswell, making your controller fat), then I'd move it out into a service - regardless of how many controllers I aim to use the functionality in. 然而,如果你发现inboundJourneys操作的代码很多(你可能想在multiStopController中做一些其他的事情,让你的控制器变胖),那么我会把它移到一个服务中 - 无论我有多少个控制器旨在使用中的功能。

Separation of concerns in mind, it is best to create a service or a factory to represent an InboundJourney object. 考虑到关注点,最好创建一个服务或工厂来表示InboundJourney对象。 This object will also have methods for manipulating the data, eg addInboundJourney(journey) . 该对象还具有操纵数据的方法,例如addInboundJourney(journey)

Which one to use is up to you - generally speaking, factory is used when pre-initialization code is required. 哪一个使用取决于您 - 一般来说,在需要预初始化代码时使用工厂。

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

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