AngularJS / Jade不使用ng-app指令

时间:2015-06-09 10:54:26

标签: angularjs node.js pug

我尝试构建一个小型原型Webserver(NodeJS / Jade)来学习AngularJS。到目前为止,我只用NodeJS / Javascript / JQuery编码。我做了以下这些块: 仅当ng-app =''(没有特定名称)时,才能正确计算短表达式{{843/42}}。当我使用ng-app ='myApp'时,表达式直接输出而不进行计算。在botuh案例中,John Doe没有填写两个文本框。任何人都可以帮助我或给我一个建议。

layout.jade

doctype html
html(ng-app='')
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
    script(src='/javascripts/angular/angular.js', type='text/javascript')
    script(src='/javascripts/angular/angular-resource.js', type='text/javascript')
    script(src='/javascripts/jquery-2.1.4.min.js', type='text/javascript')
    script(src='/javascripts/index.js', type='text/javascript')

    block scripts
  body
    block content

index.jade

extends layout

block content
  h1= title
  p Welcome to #{title}
  div
      table
        tr
          td
          | {{ 843 / 42 }}
          td
        tr
          td First Name:
          td
            input(type='text', ng-model='firstName')
        tr
          td Last Name:
          td
            input(type='text', ng-model='lastName')
        tr
          td Full Name:
          td
            | {{firstName + lastName}}
block scripts
  | <script type='text/javascript'>
  | $(document).ready(function() {
  |   Index_Load();
  | });
  | </script>

Index_Load:

function Index_Load()
{
    var app = angular.module('myApp', []);

    app.controller('myCtrl', function($scope) {
        $scope.firstName= "John";
        $scope.lastName= "Doe";
    });

}

1 个答案:

答案 0 :(得分:0)

模块myApp仅在文档就绪时初始化,而不是角度所期望的。不要在函数中定义模块和控制器并在文档准备就绪时调用它,而是使用此

block scripts
  script(type='text/javascript').
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function($scope) {
        $scope.firstName= "John";
        $scope.lastName= "Doe";
    });

这将确保您的模块已定义并可用于angularjs