2016-08-15 55 views
0

我无法弄清楚为什么我的控制器和模块没有像我一起学习的教程那样绑定。我使用的是Brackets程序,它提供了我的代码的实时预览,而不是显示$ scope.message,它只显示单词{{message}}。我刚刚开始学习angularjs。在文档的头部,我使用脚本标记和src =“https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js” 这里是body ... Angular.js绑定(简单的html文档)

您已成功地到达我的HTML文档!

<div ng-app="myModule" ng-controller="myController"> 

    <!h5 tag contains a binded expression> 

     <h5> {{message}} </h5> 
    <ul> 
     <li ng-repeat="x in cars"> {{x}} </li> 

     </ul> 


    </div> 


    <!Create a module named 'myModule'Create controller named 'myController'> 

    <script> 


    var myApp =angular.module("myModule",[]); 

    myApp.controller("myController", function ($scope){ 

     $scope.cars = ["BMW", "Toyota", "Ford", "Range Rover"]; 
     $scope.message = "My students are the best in the world!"; 

    }) 

    </script> 


</body> 
+0

是否当您在浏览器甚至'文件打开它的工作? – pr0gramist

+0

我觉得你可能会遇到错误。你有没有在控制台中看到任何东西? – 1252748

+0

它显示{{message}},但在教学视频中,他的浏览器显示实际的信息“我的学生是世界上最好的!”我正在复制他的代码行,并使用与所建议的相同的程序“Brackets”。 –

回答

0

角检测到您的ngApp之前创建的模块,因此抛出一个异常$injector:modulerr。如果你打开你的控制台,你可以看到这个。将文档中的脚本移动到应用了ngApp的容器上方可解决您的问题。

http://jsbin.com/quxinozubu/edit?html,js,output

0

你的代码是为我工作的罚款。我刚看到一个简单的错误。你错过了控制器中的分号。 /// ..`:

var myApp =angular.module("myModule",[]); 
 

 
    myApp.controller("myController", function ($scope){ 
 

 
     $scope.cars = ["BMW", "Toyota", "Ford", "Range Rover"]; 
 
     $scope.message = "My students are the best in the world!"; 
 

 
    });
<!DOCTYPE html> 
 
<html> 
 
    <head> 
 
    <meta charset="utf-8"> 
 
    <title></title> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 

 
    </head> 
 
    <body> 
 

 
    <div ng-app="myModule" ng-controller="myController"> 
 

 

 

 
     <h5> {{message}} </h5> 
 
    <ul> 
 
     <li ng-repeat="x in cars"> {{x}} </li> 
 

 
     </ul> 
 

 

 
    </div> 
 

 

 

 
    
 

 

 
</body> 
 
</html>

+0

@ Kasper_Sky,如果您认为这有帮助,请将其标记为答案。 –