如何工作NG控制器和结果未显示在浏览器上



我是Angular JS的新手。我写了一些角度代码。我还定义了控制器代码。有人可以告诉我为什么我无法获得scope变量值?我错过了什么?

html:

<!DOCTYPE html>
    <html ng-app="myApp">
      <head>
         <script src="angular.js"></script>
      </head> 
      <!--this above is external java script
          file my controller is not working, 
          what problem i am facing ?-->
    <body>
        <!-- the below is ng-controller -->
      <div ng-controller="HelloCtrl">
         say hello to : <input type="text" ng-model="name"/>
         <h1>  Hello, {{name}}! </h1>
     </div>

脚本:

    <script>
         var HelloCtrl = function ($scope) {
            $scope.name = 'World';
         }
    </script>
    </body>
</html>

您应该定义您的应用程序和控制器:

var myApp = angular.module("myApp", []);
myApp.controller("HelloCtrl", function ($scope) { 
    $scope.name = "world";
});

您缺少var myApp = angular.module('myApp',[]);

您的script应该看起来像:

<script>
 var myApp = angular.module('myApp',[]);
 var HelloCtrl = function ($scope) {
   $scope.name = 'World';
 }
</script>

工作示例。取决于角版。:)

最新更新