Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.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/1/angularjs/21.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_Angularjs_Node.js_Controller_Client Side - Fatal编程技术网

Javascript ng控制器不';即使调用正确,也无法工作

Javascript ng控制器不';即使调用正确,也无法工作,javascript,angularjs,node.js,controller,client-side,Javascript,Angularjs,Node.js,Controller,Client Side,我测试了最基本的angularjs。我使用nodejs、angularjs和html 这是我的档案节点模块未按b/c方式发布,因为它太大。 在localhost:8080中。这是我得到的输出 {{1 + 64}} {{main.msg}} {{this.msg}} {{msg}} {{mainController.msg}} 这根本不管用。但是,当我没有ng控制器和ng应用程序(初始化为nothing)时,它可以工作。但是我不能给控制器打电话。我只能做基本的{{1+64} 谁能告诉我我做

我测试了最基本的angularjs。我使用nodejs、angularjs和html

这是我的档案节点模块未按b/c方式发布,因为它太大。

在localhost:8080中。这是我得到的输出

{{1 + 64}}
{{main.msg}}

{{this.msg}}

{{msg}}

{{mainController.msg}}
这根本不管用。但是,当我没有ng控制器和ng应用程序(初始化为nothing)时,它可以工作。但是我不能给控制器打电话。我只能做基本的
{{1+64}


谁能告诉我我做错了什么吗?

您还需要将控制器分配到适当的模块

所以你的index.html说:

ng-app="firstController"
实际上,你们的模块就是这样的

var app = angular.module("firstController", []);
(旁注:我可能会将其重命名为firstApp,因为它是整个应用程序的名称)

现在,如注释中所述,将mailController重命名为mainController,它应该可以工作了。看看这个例子:

编辑:刚刚看到您正在使用express提供此服务。而您唯一公开的路由只会给出
index.html
——这意味着ngapp.js永远不会到达浏览器:

app.get('/', function( req, res) {
  res.sendFile(path.join(__dirname + '/index.html'));
});
这里有两种解决方法。您可以使用express.static而不是直接提供文件:

app.use(express.static());
或者,你根本不需要快递。您可以使用实际的HTTP服务器(如apache或nginx)为这些文件提供服务,也可以使用
HTTP服务器
节点模块:

npm install http-server
node_modules/.bin/http-server .
它将为您提供文件,而不是快递


这里还有更多的事情要做,例如将其全部移动到/public目录或类似的目录中,以将服务器模块与客户端分开,但这些与您的问题无关。

我建议您执行以下操作

在ngapp.js文件中,将其重写为:

var app = angular.module("firstController", []);
        app.controller("mainController", function($scope) {
            $scope.msg = "Hello from NG controller";
        });
在您的标记中,请重写为:

<!DOCTYPE html>
<html ng-app="firstController">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.8/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.8/angular-route.min.js"></script>
    <script src="ngapp.js"></script>
</head>

<body ng-controller="mainController">
{{1 + 64}}
<h3>{{msg}}</h3>
</body>
</html>

{{1 + 64}}
{{msg}}
提示:将其重命名为
ng app=“firstController”
,而不是
ng app=“firstApp”
,因为它是一个应用程序


检查拼写
(app.controller(“mailController”)
,在标记中有
ng controller(mainController))

你有
ng controller=“mainController as main”
app.controller(“mailController”),…
,即,
main
mail
。修复打字错误,它应该可以正常工作。它仍然不能正常工作。您是否尝试在主控制器中使用$scope而不是它?否,我没有使用scope。为什么使用$scope?$scope是一个固定版本我已将mailController更改为mainController,但它仍然不能正常工作。WtfDid您是否看到jsbin版本?如果你直接复制粘贴这些值怎么办?发现问题,更新答案。我是否只使用app.use(express.static());?express.static()也服务静态文件。这与我以前的代码有什么不同?@Zlatko