角度返回's自己的标记,而不是ejs中的输出

Angular returning it's own tags instead of output in ejs

本文关键字:ejs 输出 返回 自己的      更新时间:2023-09-26

我试着用angular在我的ejs模板上对我的输入进行一些输入验证,但它似乎不能正常工作,这是我使用的代码(来自w3schools(:

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="">
<p>Try writing in the input field:</p>
<form name="myForm">
<input name="myInput" ng-model="myInput" required>
</form>
<p>The input's valid state is:</p>
<h1>{{myForm.myInput.$valid}}</h1>
</body>
</html>

它应该输出这样的东西:

The input's valid state is: false

但它只是返回这个:

The input's valid state is: {{myForm.myInput.$valid}}

那么,有没有可能将ejs和angular一起使用呢?

您需要定义一个模块和一个控制器,脚本应该加载在主体/头部中

HTML

  <!DOCTYPE html>
    <html ng-app="store">
    <head>
      <meta charset="utf-8" />
      <title>AngularJS Plunker</title>
      <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
      <script src="app.js"></script>
    </head>
    <body ng-app="">
        <form name="myForm">
          <input name="myInput" ng-model="myInput" required>
        </form>
        <p>The input's valid state is:</p>
        <h1>{{myForm.myInput.$valid}}</h1>
        </body>
    </html>

控制器

 var app = angular.module('store', []);
 app.controller('StoreController', function($scope) {
 });

DEMO

<html ng-app="insert_app_name_here">
<body ng-controller="insert_controller_name_here">
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.js"></script>
</body>
</html>

我会像上面的代码一样设置我的angular应用程序。此外,在body标签的末尾包含脚本也是一种很好的做法。此外,将.min从脚本标记中删除为仅angular.js而不是angular.min.js有助于更好地调试代码,因为您将使用它进行开发,而不是生产。

在设置你的应用程序时,我会包含一个app.js文件,其中的代码如下:

angular.module('insert_app_name_here', [])

在设置控制器时,我会包含一个控制器文件(即mainCtrl.js(,其中的代码如下:

angular.module('insert_app_name_here').controller('insert_controller_name_here, function($scope) {
});

至于将angular与ejs联系起来,我不熟悉ejs,因此我无法提供解决方案。