小编fed*_*rom的帖子

Angular 2 - 关闭窗口时执行代码

我正在使用角度2进行聊天应用程序.

当用户关闭窗口时,如何将完成聊天命令发送到后端?

我的组件有一个方法,调用后端服务以下面的方式结束聊天

 endChat() {
        this.chatService.endChat(this.chatSessionInfo).subscribe(
            result => this.OnGetMessages(result),
            error => this.OnChatEndError(error)
        );
    }
Run Code Online (Sandbox Code Playgroud)

关闭窗口时如何执行该代码?如何检测窗口关闭事件?

我尝试使用ngOnDestroy但由于某种原因代码没有被执行.

在我的Component.ts中我有.

import { Component, OnInit, AfterViewChecked, ElementRef, ViewChild,OnDestroy} from '@angular/core';

export class ChatComponent implements OnInit, AfterViewChecked,OnDestroy  {
Run Code Online (Sandbox Code Playgroud)

最后

 ngOnDestroy() { 
    this.endChat();
 }
Run Code Online (Sandbox Code Playgroud)

谢谢!

typescript angular

28
推荐指数
2
解决办法
4万
查看次数

AngularJS - Factory - TypeError:无法读取未定义的属性'getSpec'

我从AngularJS开始,我在尝试使用控制器中的工厂时遇到了一些问题.

我有以下工厂

angular.module('testingApp')
  .factory('factoryService', function ($http) {
    // Service logic
    var getSpec = function(p) {
      return $http.get('http://someurl//?p=' + p);
    };
    return {
      getSpec: getSpec  
    };
  });
Run Code Online (Sandbox Code Playgroud)

然后我尝试从控制器消耗它,如下所示

angular.module('testingApp')
  .controller('ServiceincientsCtrl',[ function (factoryService,$scope) {
   console.log('Starting Service Incident Controller');
    factoryService.getSpec('AAA').then(function(response){
        $scope.result = response.data;
    }, function(error){
        console.log('opsssss' + error);
    });

  }]);
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试运行它时,我收到以下消息

TypeError: Cannot read property 'getSpec' of undefined
Run Code Online (Sandbox Code Playgroud)

我不知道我错过了什么,它应该是一个新手错误,我用谷歌搜索它,我尝试了许多具有相同结果的例子.

我做错了什么想法?

谢谢!

angularjs angularjs-factory

5
推荐指数
1
解决办法
4万
查看次数