2013-03-05 108 views
-1

我正在學習Knockoutjs並懷疑如何顯示observablearray()的值;knockoutjs observablearray not displayed

<script> 

     function Cliente(nome, CGC) { 
      this.Nome = nome; 
      this.Cgccfo = CGC; 

     }; 

     function ordemservicoVM() { 
      self = this;     
      self.pessoas = ko.observableArray([ 
       new Cliente("ValueOne", "ValueTwo")    
      ]);    
     }; 

$(document).ready(function() {   

      ko.applyBindings(new ordemservicoVM()); 
} 

</script> 

HTML代碼::在

我的JS代碼

<table>  
    <thead> 
     <tr> 
      <th>Nome</th> 
      <th>CGC</th> 
     </tr> 
    </thead> 
    <tbody data-bind="foreach: pessoas" > 
     <tr> 
      <td data-bind="text: Nome"></td> 
      <td data-bind="text: Cgccfo"></td> 
     </tr> 
    </tbody> 
</table> 

已經使用了相同的結構,上面的代碼,它完美地工作。

+1

是否有任何javascript錯誤? – 2013-03-05 16:29:17

+2

你說它工作完美,它確實。你的問題是什麼? – Tyrsius 2013-03-05 16:30:45

+0

@Tyrsius - 其他代碼完美工作,但沒有在上面的示例 – JulioCes 2013-03-05 16:38:47

回答

0

您在$(document).ready函數後缺少右括號。下面更正了片段。注意最後一行的差異:

$(document).ready(function() {   
    ko.applyBindings(new ordemservicoVM()); 
}); 

添加括號後,您的代碼開始正常工作。

它實際上在chrome開發工具的控制檯中顯示「Uncaught SyntaxError:Unexpected end of input」錯誤。因此,請注意控制檯以解決此類問題。