订阅Angular 2

时间:2016-06-09 08:32:37

标签: angular subscribe

我想让我的ngOnInit函数做下一件事:   - 使用this.structureRequest.sendRequest()对某些数据进行http请求,这样可以正常工作,并且在收到数据后开始使用this.viewNodes()函数查看它。 我使用订阅,但它不起作用,我认为我做订阅功能有问题。请帮助:)

  1. HomeComponent.ts

     angular.module('App', [])
     .controller('myCtrl', function($scope) {
      $scope.checked = true;
     })
    .directive('ngBatchIf', function($compile) {
    return {
      restrict: 'A',
      scope: {
        check: '@'
      },
      controller: function($scope) {},
      link: {
        pre: function(scope, elm, attrs) {
            attrs.$observe('check', function() {
              // After flattening, Angular will still have the first element
              // bound to the old scope, so we create a temporary marker 
              // to store it
              var contents = elm.contents();
              if (scope.check === "true") {
              console.log(scope.check);
              } else {
                                console.log(scope.check);
                elm.replaceWith(contents);
              }
            })
          }
    
       }
      }
      }); 
    
  2. 2.这是http服务,http get工作正常,收到所有数据:

    import {Component} from '@angular/core';
    import {Observable} from 'rxjs/Observable';
    import {StructureRequestService} from './StructureRequestService';
    
    
    export class Content {
    
    
    ok: boolean;
    content = [];
    }
    @Component({
    providers: [StructureRequestService],
    styleUrls: ['app/home/home.css'],
    templateUrl:'./app/home/homePageTemplate.html'
    
    })
    
    export class HomeComponent {
    contentArray = [];
    myRes: Content;
    showAssigned:boolean = false;
    showSubitems:boolean = false;
    showUsers:boolean = false;
    
    constructor(private structureRequest: StructureRequestService) {}
    
    ngOnInit() {
    this.structureRequest.sendRequest().subscribe( this.viewNodes());
    
    }
    
    viewNodes() {
    this.myRes = this.structureRequest.result;
    this.contentArray = this.myRes.content;
    this.showAssigned = true;
     }
     }
    

    3.所以问题是制作同步步骤:接收数据,而不是查看数据。

1 个答案:

答案 0 :(得分:9)

您可能希望在请求返回值时执行this.viewNodes而不执行this.viewNodes()的结果

this.structureRequest.sendRequest().subscribe( this.viewNodes());

应改为

this.structureRequest.sendRequest().subscribe(() => this.viewNodes());

前者执行this.viewNodes()并将结果传递给subscribe(),后者创建一个新的内联函数,传递给subscribe()。调用此内联函数时,会执行this.viewNodes()

如果要传递值sendRequest(),则应返回

this.structureRequest.sendRequest().subscribe((result) => this.viewNodes(result));

<强>更新

sendReqeust()不返回任何内容。

应该是

return this.http.get(this.myUrl, options) ...

但是只有在代码返回Observable时才会在代码中使用它。

但最后的subscribe()会返回Subscription

 return this.http.get(this.myUrl, options)
    .map((res: Response) => res.json())
    .subscribe(res => {this.result = res;
        return this.result; });

因此应将其更改为

 return this.http.get(this.myUrl, options)
    .map((res: Response) => res.json())
    .map(res => {
      this.result = res;
      return this.result; 
    });

 return this.http.get(this.myUrl, options)
    .map((res: Response) => res.json())
    .do(res => {
      this.result = res;
    });

不同之处在于do()不会修改流的值,也不需要返回任何内容。从.map()返回的值将被转发。如果您要使用do,请确保导入map