简体   繁体   English

AngularJS的返回/返回按钮

[英]Back/Return button with AngularJS

I'm very new in Angular and have a problem. 我是Angular的新手,有问题。

I have page with list of a items (some appointments). 我的页面上有一个项目列表(一些约会)。

AppointmentsSummary.chtml 约会Summary.chtml

<div class="border-section">
    <table>
        <thead>
        ...
        </thead>
        <tbody>
         <tr ng-repeat="app in appts">
          <td>
            <a href="#/Appointments/AppointmentsSummary/MessageHistory/{{app.patientId}}">
                 <button type="button">Message history</button>
            </a>
          </td>
         </tr>
        </tbody>
    </table>
</div>

And I have a controller for this template: 我有一个用于此模板的控制器:

function AppointmentsSummaryController($scope, $location, AppointmentResource) {
 console.log("loaded..");
 $scope.appts = AppointmentResource.getAll({
           .....(params)
        });

}

When I clicked on the button "Message history" on the AppointmentsSummary.html - I relocate to page MessageHistiry.html, which has a "Back" button. 当我单击AppointmentsSummary.html上的“消息历史记录”按钮时-我重新定位到MessageHistiry.html页面,该页面具有“后退”按钮。

<a href="#/Appointments/AppointmentsSummary">
    <button type="button">Back</button>
</a>

When I push this button, I return to list of appointments and AppointmentsControllerSummary reloaded and $scope.appts becomes null. 当我按下此按钮时,我返回到约会列表并重新加载了AppointmentsControllerSummary,并且$ scope.appts变为null。

For routing between pages I uses $routeProvider (same with below) 对于页面之间的路由,我使用$ routeProvider(与下面相同)

$routeProvider.when(/Appointments/AppointmentsSummary/MessageHistory/:patientId', {
                        controller:'MessageHistoryController',
                        templateUrl:'Template/MessageHistory',
                        reloadOnSearch: false

Can I not reload this controller and save my $scope data? 我可以不重新加载该控制器并保存$ scope数据吗?

You need to save your data in a service. 您需要将数据保存在服务中。 Services are singletons, meaning they always return the same instance and therefore preserve state. 服务是单例,这意味着它们始终返回相同的实例,因此保留状态。 Controllers get re-instantiated each time they are loaded and therefore do not preserve state. 控制器在每次加载时都会重新实例化,因此不会保留状态。

myApp.factory('MyService', function() {
    var appts = AppointmentResource.getAll({
       .....(params)
    });

    return {
       appts: appts
    };
});

Then in your controller, you can just do.. 然后在您的控制器中,您可以做..

$scope.appts = MyService.appts;

The appts variable in the service won't get reloaded when you reload controllers and the data will be preserved. 当您重新加载控制器时,服务中的appts变量将不会重新加载,并且数据将被保留。 From the angular docs ... 角度文档 ...

"Lastly, it is important to realize that all Angular services are application singletons. This means that there is only one instance of a given service per injector." “最后,重要的是要意识到所有Angular服务都是应用程序单例。这意味着每个注入器只有给定服务的一个实例。”

Often when preserving state is the issue, singletons are the solution. 通常,当要保留状态时,单例就是解决方案。

Thanks to Charlie Martin for idea with singleton-service. 感谢Charlie Martin对于单例服务的想法。

I create appointmentService 我创建约会服务

.factory('appointmentService', function (AppointmentResource, dateSharedService, doctorSharedService) {

    appointmentService = {};

    appointmentService.reloadAppts = function() {
        appointmentService.appts = AppointmentResource.getAll({
            filterFirstDate: dateSharedService.firstDate,
            filterSecondDate: dateSharedService.secondDate,
            doctorIds: doctorSharedService.doctorIds
        });
    };

    appointmentService.appts = AppointmentResource.getAll({
        filterFirstDate: dateSharedService.firstDate,
        filterSecondDate: dateSharedService.secondDate,
        doctorIds: doctorSharedService.doctorIds
    });

    return appointmentService;
});

And, so, this is part of code from my controller: 因此,这是控制器代码的一部分:

function AppointmentsSummaryController($scope, appointmentService) {
    $scope.appts = appointmentService.appts;

    $scope.$on('firstDateChanges', function () {   
        appointmentService.reloadAppts();
        $scope.appts = appointmentService.appts;
    });

    $scope.$on('secondDateChanges', function () {
        appointmentService.reloadAppts();
        $scope.appts = appointmentService.appts;
    });
    .....
}

When I load my controller at first, I get default appointments, which will today. 最初加载控制器时,将获得默认约会,该约会将在今天开始。 If params from scope changes - appointmentService get it from another injected services (dateSharedService, doctorSharedService). 如果作用域中的参数发生了变化,则约会服务会从另一个注入的服务(dateSharedService,doctorSharedService)中获取约会服务。

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

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