我的项目是nodeJs + angularJs + html。

main.html

  <html>
     <head>
       <script src="angularJs.js"></script>
       <script src="ui-router.js"></script>
      </head>
    <body ng-app="myApp">
      <div ui-view></div>
    </body>
 <script>
    var myApp = angular.module('myApp',['ui.router']).config(
           function($stateProvider,$urlRouterProvider,$httpProvider){
               $stateProvider.state('index',{
               url:'/child',
               templateUrl: '/child'
              }
       )});
  </script>
</html>


MyProject / routes / index.js

var express = require('express');
var router = express.Router();
router.get('/child',function(req, res, next) {
res.render('back/childPage', { title: 'Express' });


});

MyProject / views / back / childpage.html

<div>
    fsdfsfsafsfsa214324234234@@
</div>
<script type="text/javascript">alert(1);</script>


MyProject / app.js

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var bodyParser = require('body-parser');
var ejs = require('ejs');
// view engine setup
 app.engine('.html', ejs.__express);
app.set('view engine', 'html');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static('public'));

var routes = require('./routes/index');
app.use('/', routes);
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);


当我访问page(http://localhost:3000/#/child)时,childPage.html中的javascript代码将无法工作。
如果我仅访问nodeJs的路由器页面(http://localhost:3000/child),则javascript代码运行良好。

如果有人知道,请回答我。如果您的项目中需要更多代码,请告诉我。谢谢。

最佳答案

哈哈哈哈哈哈,我得到答案,只需添加一个控制器。

var myApp = angular.module('myApp',['ui.router']).config(
       function($stateProvider,$urlRouterProvider,$httpProvider){
           $stateProvider.state('index',{
           url:'/child',
           templateUrl: '/child'
           controller:function(){
             //you can operate element in childPage.html
            }
          }
   )});

07-24 16:52