Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用ng控制器,且结果未显示在浏览器上?_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 如何使用ng控制器,且结果未显示在浏览器上?

Javascript 如何使用ng控制器,且结果未显示在浏览器上?,javascript,html,angularjs,Javascript,Html,Angularjs,我是新手。我写了一些角度代码。 我还定义了控制器代码。有人能告诉我为什么我不能得到范围变量值吗? 我错过了什么吗 HTML: <!DOCTYPE html> <html ng-app="myApp"> <head> <script src="angular.js"></script> </head> <!--this above is external j

我是新手。我写了一些角度代码。 我还定义了控制器代码。有人能告诉我为什么我不能得到
范围
变量值吗?
我错过了什么吗

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>

向……问好:
你好,{{name}}!
脚本:

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

    </body>
</html>

var HelloCtrl=函数($scope){
$scope.name='World';
}

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

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

myApp.controller("HelloCtrl", function ($scope) { 
    $scope.name = "world";
});

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

您的
脚本应该如下所示:

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

var myApp=angular.module('myApp',[]);
var HelloCtrl=函数($scope){
$scope.name='World';
}

工作。取决于角度版本。:)

可能取决于角度,我想是的@Sourabh@Sourabh-我不是一个悲观的选民。尽管您的小提琴可以工作,但使用控制器功能污染全局(窗口)范围是一种不好的做法。这不是我们想要的方式。此外,在JSFIDLE上还存在两个AngularJS版本。因此,您不需要为此框架提供外部链接:)@KirillSlatin。你的错误做法可能是正确的。我编辑了一个现有的小提琴,外部链接已经存在,所以我选择不删除它,很简单:)