Angular2 - 将'activatedRoute.params'变成承诺

时间:2016-10-06 17:27:11

标签: javascript angular promise angular-promise observable

我试图把我从ActivatedRoute下来的params的观察变成一个承诺,但没有任何运气。我已成功将http个请求转换为承诺:

this.riaService.getAdvisorsForState(this.activeState)
    .then(rias => {
        this.riasForState = rias.json();
        console.log(this.riasForState);
    });
// this all works ^

但是我无法将'activatedRoute.params'转变为承诺:

export class InvestmentAdvisorStateComponent implements OnInit {

    constructor(private activatedRoute: ActivatedRoute, private riaService: InvestmentAdvisorService) { }

    getStateFromUrl() {
        this.activatedRoute.params
            .toPromise()
            .then(params => {
                console.log('HERE',params)
            });
    }
// this does not work for me ^

这就是我现在所拥有的:

getStateFromUrl() {
    this.activatedRoute.params
        .subscribe((param: any) => {
            this.activeState = param['state'];
        });
}
// works ^

我希望将此作为一个承诺来实现,这样我就可以.then了。任何有关如何做到这一点的帮助非常感谢。

1 个答案:

答案 0 :(得分:6)

observable和promise之间的主要区别在于,observable可以发出多个事件,而promise只发出一个事件。

为了让您的示例有效,我假设您只对第一个事件感兴趣。

import java.util.NoSuchElementException;


public class LinkedList<AnyType> {
public class Node<AnyType>
{
private AnyType data;
private Node<AnyType> next;
public Node(AnyType data, Node<AnyType> next)
{
this.data = data;
this.next = next;
}

/**
 * @return the data
 */
public AnyType getData() {
    return data;
}

/**
 * @param data the data to set
 */
public void setData(AnyType data) {
    this.data = data;
}

/**
 * @return the next
 */
public Node<AnyType> getNext() {
    return next;
}

/**
 * @param next the next to set
 */
public void setNext(Node<AnyType> next) {
    this.next = next;
}


}

 private Node<AnyType> head;

/*
*  Constructs an empty list
*/
public LinkedList()
{
head = null;
}




 public boolean isEmpty()
 {
   return head == null;
 }
 /*
 *  Inserts a new node at the beginning of this list.
 *  @param item to be inserted in First
 */
  public void insertFirst(AnyType item)
  {
  head = new Node<AnyType>(item, head);
  }
  /*
  *  Returns the first element in the list.
  *  @return First element in linked List
  */
  public AnyType getFirst()
  {
  if(head == null) throw new NoSuchElementException();
  return head.getData();
  }
/*
 *  Removes the first element in the list.
 *  @return Deleted First Item
 */
  public AnyType deleteFirst()
  {
    AnyType tmp = getFirst();
    head = head.getNext();
    return tmp;
  }
 /*
  *  Recursively inserts a new node to the end of this list.
  *  @param item to be inserted in last
  */
  public void insertLast(AnyType item)
  { 
       if( head == null)
        insertFirst(item);
       else
        insertLast(head, item);
  }
  private void insertLast(Node<AnyType> node, AnyType item)
  {
    if(node.getNext() != null) insertLast(node.getNext(), item);
    else
    node.setNext( new Node<AnyType>(item, null));
  }

这样,使用 /* * Display all the elements in linked list */ public void DisplayList() { Node<AnyType> Current=head; while(Current!=null) { System.out.print("[ "+ Current.getData()+" ] -> ");//want to print name of the book here //i do the following but not worked Current.getData().getName(); Current=Current.getNext(); } System.out.println("NULL"); } 创建的承诺就会在第一个getStateFromUrl() { this.activatedRoute.params .first() .toPromise() .then(params => { console.log('HERE',params) }); } 事件中完成。