gpt4 book ai didi

javascript - 使用 Express JS 的 Angular 模板组件

转载 作者:行者123 更新时间:2023-12-03 06:40:19 24 4
gpt4 key购买 nike

我正在尝试在 Angular 中的一个文件夹中加载模板文件,并且我已经使用 Express 静态调用我的 JS 文件。由于某种原因,我的模板文件无法加载。我尝试将它们与我的 index.html 文件(因为它们是相对于 .html 文件调用的)以及它们自己的文件夹一起放在我的 View 文件夹中,并使路径与静态调用/js 时相同。我应该做什么来使用我的 Angular 模板加载路线覆盖 Express 路线?

即:templateUrl: '/latest-purchases/latest-purchases.template.html',& templateUrl: 'latest-purcahses.template.html',//当与index.html在同一文件夹中时,两者都不会加载。

Express JS 文件

var express     = require('express'),
app = express(),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
path = require('path');

//Connect Mongoose DB To Database
mongoose.connect('mongodb://localhost:27017/finance');

app.use(bodyParser.urlencoded({
extended: true
}));

app.use(bodyParser.json());

//Use pathjoin: takes care of unecessary delimiters(occurence: pathes from unknown sources)
//i.e.,: 'a','/b' or '/a','b' or 'a','b' etc...
app.use('/bower_components', express.static(path.join(__dirname, '/bower_components')));

app.use('/js', express.static(__dirname + '/client/js'));

app.get('/', function(request, response){
response.sendFile(__dirname + '/client/views/index.html');
});

app.listen(3000, function() {
console.log("Listening on 3000...");
});

index.html

  <body ng-app="financeApp>

<div>
<latest-purchase></latest-purchase>
</div>

<!-- Keep @ Bottom For Better Load Times -->
<script src="/bower_components/angular/angular.js"></script>
<script type="text/javascript" src="/js/app.module.js"></script>
<script type="text/javascript" src="/js/latest-purchases/latest-purchases.module.js"></script>
<script type="text/javascript" src="/js/latest-purchases/latest-purchases.component.js"></script>
</body>
</html>

最新购买.component.js

angular.
module('latestPurchases').
component('latestPurchases', {
templateUrl: 'latest-purchases.template.html',
controller: function latestPurchaseController() {
this.purchases = [
{
name: 'Television',
price: 40.00,
date: 'Septmber 12th, 2014'
}, {
name: 'Skateboard',
price: 50.00,
date: 'October 6, 1993'
}, {
name: 'iPhone',
price: 200.99,
date: 'October 30th, 2014'
}
];
}
});

最新购买.template.html

<table>
<tr>
<th> Name </th>
<th> Price </th>
<th> Date </th>
</tr>
<tr ng-repeat="purchase in $ctrl.purchases">
<td>{{purchase.name}}</td>
<td>{{purchase.price}}</td>
<td>{{purchase.date}}</td>
</tr>
</table>

app.module.js

var app = angular.module('financeApp', [
'latestPurchases'
]);

目录树:

/app
/server.js
/client
/js
/latest-purchases
/latest-purchases.component.js
/latest-purchases.component.spec.js
/latest-purchases.template.html.js
/latest-purchases.module.js
/views
/index.html
/app.module.js

最佳答案

我会将index.html移动到/client和server.js中,在self.initializeServer = function()中我调用:

self.app.use(express.static('client'));

所以,如果我去 http://127.0.0.1:8080/ ,它会打开index.html。要包含子文件夹中的文件,我使用以下命令:

<script src="js/latest-purchases/latest-purchases.module.js"></script>

请告诉我们。如果这不起作用,我可以为您完成一个 github。最好的。

关于javascript - 使用 Express JS 的 Angular 模板组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37994902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com