2016-03-02 72 views
1

我是新來的角。到目前爲止,代碼一直工作正常,但當我使用ng-app =「demoApp」或任何其他應用程序指令時,它似乎停止工作。它不能綁定在控制器上。這是我的看法:angular ng-app =「demoApp」不適用於我

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8"> 
    <title>Learn AngularJS->Binding on the Controllers</title> 

</head> 

<body> 

<div class="container" ng-app="demoApp" data-ng-controller="simpleController"> 
<h3>Adding a simple Controller</h3> 

<ul> 

<li data-ng-repeat="cust in customers"> 
{{cust.name}} - {{cust.city}} 

</li> 

</ul> 

</div> 

這是我的js /控制器/ simpleController.js

var app = angular.module('demoApp',[]); 
    var app = angular.module('demoApp',[]); 
app.controller('simpleController',function($scope) 
{ 
$scope.customers=[ 
{name:'John Smith',{city:'Kitale'}, 
{name:'Jane Martins',{city:'Bungoma'}, 
{name:'Esther Williams',{city:'Busia'}, 
{name:'James Anthony',{city:'Nakuru'}, 
{name:'Irine seniorman',{city:'Eldoret'}, 
{name:'Agrey Ngoya',{city:'Kitui'}, 
{name:'Anne Chemos',{city:'Bomet'}, 
{name:'Zacheous Waweru',{city:'Murang\'a'} 

]; 

}); 
<script type="text/javascript" src="../js/angular-1.5.0/angular.min.js"></script> 

<script type="text/javascript" src="../js/controller/simpleController.js"></script> 

這是包括NG-應用= demoApp」指令爲表示以後有什麼我得到在瀏覽器我的上述觀點:

Adding a simple Controller 

{{cust.name}} - {{cust.city}} 

但是當我刪除NG-應用=「demoApp」的其他代碼只是正常的東西,我不明白是NG-應用指令不是爲我工作的其他指令。在工作中好,但也停止工作的應用程序指令在代碼中的任何地方使用。我已經檢查過,以確保app代碼不會在代碼中使用兩次,但是根本沒有成功。請任何幫助,我可能會出錯。我找不到任何錯誤。

+0

像在'' –

+0

'看到下面的答案。也許這會幫助你,你爲$ scope.customers定義是錯誤的。你可以檢查你的代碼,看看你可以用你的代碼做什麼。 upvote和滴答正確,使幫助者想幫助更多:) –

回答

1

您的客戶對象中存在一些錯誤複製粘貼下面的代碼並再次嘗試。

的Html

<body ng-app="demoApp"> 
    <div class="container" ng-controller="simpleController"> 
     <h3>Adding a simple Controller</h3> 

     <ul> 

     <li data-ng-repeat="cust in customers"> 
     {{cust.name}} - {{cust.city}} 

     </li> 

     </ul> 

    </div> 
    </body> 

Controller.js

var app = angular.module('demoApp',[]); 

app.controller('simpleController',['$scope', function($scope){ 
    $scope.customers=[ 
    {name:'John Smith',city:'Kitale'}, 
    {name:'Jane Martins',city:'Bungoma'}, 
    {name:'Esther Williams',city:'Busia'}, 
    {name:'James Anthony',city:'Nakuru'}, 
    {name:'Irine seniorman',city:'Eldoret'}, 
    {name:'Agrey Ngoya',city:'Kitui'}, 
    {name:'Anne Chemos',city:'Bomet'}, 
    {name:'Zacheous Waweru',city:'Murang\'a'} 
    ]; 
}]); 
+0

非常感謝,它現在的作品。這只是你指出的問題。 – wafutech

0

你的數組定義是錯誤的。

NO:{name:'John Smith',{city:'Kitale'}
YES:{name:'John Smith',city:'Kitale'}

這裏是工作Plunker:http://plnkr.co/edit/hC3iY8HUG23N7DjIS2hP?p=preview

所以,你的整體代碼:

<!DOCTYPE html> 
<html> 

<head> 
    <script data-require="[email protected]*" data-semver="1.3.0-beta.5" src="https://code.angularjs.org/1.3.0-beta.5/angular.js"></script> 
    <link rel="stylesheet" href="style.css" /> 
    <script src="script.js"></script> 
</head> 

<body> 
    <div class="container" ng-app="demoApp" data-ng-controller="simpleController"> 
     <h3>Adding a simple Controller</h3> 
     <ul> 
      <li data-ng-repeat="cust in customers"> 
       {{cust.name}} - {{cust.city}} 
      </li> 
     </ul> 

    </div> 
    <script> 
     var app = angular.module('demoApp', []); 
     app.controller('simpleController', function($scope) { 
      $scope.customers = [{ 
        name: 'John Smith', 
        city: 'Kitale' 
       }, { 
        name: 'Jane Martins', 
        city: 'Bungoma' 
       }, { 
        name: 'Esther Williams', 
        city: 'Busia' 
       }, { 
        name: 'James Anthony', 
        city: 'Nakuru' 
       }, { 
        name: 'Irine seniorman', 
        city: 'Eldoret' 
       }, { 
        name: 'Agrey Ngoya', 
        city: 'Kitui' 
       }, { 
        name: 'Anne Chemos', 
        city: 'Bomet' 
       }, { 
        name: 'Zacheous Waweru', 
        city: 'Murang\'a' 
       } 

      ]; 

     }); 
    </script> 
</body> 

</html> 
+0

謝謝@Alex Rumba Nicked。有用。我真的很感謝你的幫助。 – wafutech

+0

很高興我可以得到一些幫助。 –