我对Angular.js非常陌生,我似乎无法让我的表达式来显示控制器中的值,当我查看它时,它只是将其视为字符串。我不知道为什么。我是否错过了一些非常明显的东西?我们将不胜感激。
我也只是使用node.js设置本地服务器,但我不认为这是问题所在。
var appdemo = angular.module('appdemo', []);
appdemo.controller('ctrl', function($scope) {
$scope.value1 = 1;
$scope.value2 = 2;
$scope.updateValue = function() {
$scope.x = $scope.value1 + ' x ' + $scope.value2 +
" = " (+$scope.value1 + +$scope.value2)
};
});
<!DOCTYPE html>
<html ng-app="appdemo">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>appdemo</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div ng-controller="ctrl">
<span>times</span>
<input type="text" ng-model="value1" />
<input type="text" ng-model="value2" />
<button ng-click="updateValue()">total</button> {{x}} {{ value1 }}
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js">
</script>
<script src="main.js"></script>
</body>
</html>
const express = require('express')
const app = express()
app.use(express.static("public"))
app.engine("html", require("ejs").renderFile)
app.set("view engine", "html")
app.set("views", "./views")
app.get("/", (req, res) =>{
res.render("index")
})
app.listen(3000, ()=>{
console.log("Running on port 3000...")
})
文件结构:
node_mdodules
public
css
views
index.html
main.js
app.js
package-lock.json
package.json
答案 0 :(得分:0)
假设您具有下一个文件夹结构:
-App name
-public
-html
-index.html
-js
-main.js
-style.css
-server.js //Or any other name for your node js server
在服务器js中,您应该使用__dirname
属性:
app.use(express.static(__dirname + '/public'));
然后在html中替换为此的导入脚本:
<script src="/js/main.js"></script>