2014-11-17 5 views
4

저는 최근에 재스민 프레임 워크로 각도기를 사용하기 시작했으며 자바 스크립트도 새로 도입했습니다. 10 개의 속성이있는 페이지 중 하나에 대해 각 필드에 대해 getText를 사용하고 개체의이 값을 업데이트하려고합니다. 다음은 예제 코드입니다각도기에서 객체 속성을 업데이트하는 방법은 무엇입니까?

Var obj ={} 

It('should get the value,function(){ 
    Element(by.id("id of firstfield")).getText().then(function(text){ 
     obj.firstfield = text 
     Console.log(obj) //this correctly prints the updated object 
    }) 

    Console.log(obj) // prints empty object. I need to get updated object here. 

    Element (by.id("id of secondfield")).getText().then(function(text){ 
     obj.secondfield = text 
     Console.log(obj) //this correctly prints the updated object 
    }) 
}) 

어떻게 값을 바꿀 수 있습니까?

+0

Promises가 JavaScript로 작동하는 방법을 읽는 데 도움이되는 것처럼 들리는데, 이는 좋은 시작 리소스입니다. http://www.html5rocks.com/en/tutorials/es6/promises/ – Jmr

답변

1

블록 내부의 변수에 대한 변경 사항은 블록 외부에 반영되지 않습니다. .then 블록 외부에서 변경 내용을 볼 수있게하려면 return 문을 사용하고 변수에 반환 된 값을 수집합니다.

돌아 가기 업데이트 된 OBJ 가치와 다음이 도움이된다면보기 그 때는

obj = Element(by.id("id of firstfield")).getText().then(function(text){ 
    obj.firstfield = text 
    Console.log(obj) //this correctly prints the updated object 
    return obj; 
}) 

를 통해 인쇄!

+0

이유에 대한 자세한 설명 추가 현재의 접근법은 더 좋은 대답이 될 것입니다. – alecxe

+0

추가 된 설명! 희망이 지금 더 의미가 있습니다. –

+0

이 작업을하지 못했습니다. 결과는 {then [function : then], cancel : [기능 : 취소], isPending : [기능 : isPending] – AAG

관련 문제