2015-05-19 59 views
0

我想按如下方式将值推入数组,但它显示为空数组。警报工作。我相信我错过了一些明显的东西。任何线索?我已经包含了只有阵列推送尝试的代码和另一个有工作警报的代码条目。AngularJS阵列不加载在视图加载,但警报/控制台工作

$scope.termsArray = []; 

    execOperation(); 

    function execOperation() { 
     //Current Context 
     var context = SP.ClientContext.get_current(); 
     //Current Taxonomy Session 
     var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context); 
     //Term Stores 
     var termStores = taxSession.get_termStores(); 
     //Name of the Term Store from which to get the Terms. 
     var termStore = termStores.getByName("Taxonomy_111111111111"); 
     //GUID of Term Set from which to get the Terms. 
     var termSet = termStore.getTermSet("12345-55-689"); 
     var terms = termSet.getAllTerms(); 
     context.load(terms); 
     context.executeQueryAsync(function() { 
      var termEnumerator = terms.getEnumerator(); 
      while (termEnumerator.moveNext()) { 
       var currentTerm = termEnumerator.get_current(); 
       $scope.termsArray.push({ 
           termName: currentTerm.get_name(), 
           termGUID: currentTerm.get_id(), 
           termSynonyms: 'Coming Soon' 
          }); 
      } 

     }, function (sender, args) { 
      console.log(args.get_message()); 
     }); 
    } 

这里是工作的警报代码:

function execOperation() { 
     //Current Context 
     var context = SP.ClientContext.get_current(); 
     //Current Taxonomy Session 
     var taxSession = SP.Taxonomy.TaxonomySession.getTaxonomySession(context); 
     //Term Stores 
     var termStores = taxSession.get_termStores(); 
     //Name of the Term Store from which to get the Terms. 
     var termStore = termStores.getByName("Taxonomy_111111111111"); 
     //GUID of Term Set from which to get the Terms. 
     var termSet = termStore.getTermSet("12345-55-689"); 
     var terms = termSet.getAllTerms(); 
     context.load(terms); 
     context.executeQueryAsync(function() { 
      var termEnumerator = terms.getEnumerator(); 
      var termList = "Terms: \n"; 
      while (termEnumerator.moveNext()) { 
       var currentTerm = termEnumerator.get_current(); 
       $scope.termsArray.push({ 
        termName: currentTerm.get_name(), 
        termGUID: currentTerm.get_id(), 
        termSynonyms: 'Coming Soon' 
       }); 

       termList += currentTerm.get_id() + currentTerm.get_name() + "\n"; 
      } 
      alert(termList); 
     }, function (sender, args) { 
      console.log(args.get_message()); 
     }); 
    } 

下面是HTML输出:

<div> {{termsArray}}</div> 

它返回[]

更新:如果我点击在一个输入框中退出,或者点击一个提醒然后点击确定,然后加载数组而不是在页面加载时加载。

+0

您在哪里尝试显示它?你在哪里放置工作警报? – bviale

+0

其中是'''termName:currentTerm。 termGUID:currentTerm。 termSynonyms:'即将来临'来自?你在这里错过了一些代码。最后放上'''console.log($ scope.termsArray)'''让我们看看返回的结果。并且,把这个'''execOperation();''放在函数的末尾。 – NietzscheProgrammer

+0

看起来像一个异步问题。你在哪里显示内容和如何 –

回答