简体   繁体   中英

Problems with $scope in Angular.JS in Jade

I am pretty new t Angular JS, and am trying to implement this example from the Angular documentation .

However, nothing is rendering in the pre elements. Not even empty curly braces. Have I made a mistake? Or is there something special I need to account for with Jade?

layout.jade

doctype html
html(ng-app)
    head
        meta(charset="utf-8")
        meta(http-equiv="X-UA-Compatible" content="IE=edge")
        title SpaceLab
        meta(name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no")
        link(rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon")
        link(rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css")
        link(rel="stylesheet" href="/stylesheets/font-awesome.min.css")
        link(rel="stylesheet" href="/stylesheets/animate.css")
        link(rel="stylesheet" href="/stylesheets/main.css")
        link(rel="stylesheet" href="/plugins/jvectormap/css/jquery-jvectormap-1.2.2.css")
        link(rel="stylesheet" href="/plugins/todo/css/todos.css")
        link(rel="stylesheet" href="/plugins/morris/css/morris.css")
        link(href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,900,300italic,400italic,600italic,700italic,900italic' rel='stylesheet' type='text/css')
        link(href='http://fonts.googleapis.com/css?family=Open+Sans:400,700' rel='stylesheet' type='text/css')
        script(src="/javascripts/modernizr-2.6.2.min.js")

    body
        section#container
            header#header
                .brand
                    a(href="index.html" class="logo" style="padding-left: 22px;")
                        | <span>Fit</span>Essentials
                .toggle-navigation.toggle-left
                    button(type="button" class="btn btn-default" id="toggle-left" data-toggle="tooltip" data-placement="right" title="Toggle Navigation")
                        i.fa.fa-bars
                .user-nav
                    ul
                        li.profile-photo
                            img(src="/images/avatar.jpeg" class="img-circle")
                        li.dropdown.settings
                            a(class="dropdown-toggle" data-toggle="dropdown" href="#")
                                | Drew Wyatt <i class="fa fa-angle-down"></i>
                            ul.dropdown-menu.animated.fadeInDown
                                li
                                    a(href="#")
                                        | <i class="fa fa-user"></i> Profile
                                li
                                    a(href="#")
                                        | <i class="fa fa-calendar"></i> Calendar
                                li
                                    a(href="#")
                                        | <i class="fa fa-power-off"></i> Logout
            aside.sidebar
                div(id="leftside-navigation" class="nano")
                    include navigation.jade
            section.main-content-wrapper
                section#main-content
                    block content
        script(src="/javascripts/jquery-1.10.2.min.js")
        script(src="/javascripts/angular.js")
        script(src="/plugins/bootstrap/js/bootstrap.min.js")
        script(src="/plugins/waypoints/waypoints.min.js")
        script(src="/plugins/nanoScroller/jquery.nanoscroller.min.js")
        script(src="/javascripts/application.js")
        script(src="/plugins/countTo/jquery.countTo.js")
        script(src="/plugins/weather/js/skycons.js")
        script(src="/plugins/jvectormap/js/jquery-jvectormap-1.2.2.min.js")
        script(src="/plugins/jvectormap/js/jquery-jvectormap-world-mill-en.js")
        script(src="/plugins/todo/js/todos.js")
        script.
            $(document).ready(function() {
                app.timer();
                app.map();
                app.weather();
            });

user-form.jade

extends layout

block content
    .row
        .col-md-12
            div(ng-controller="Controller")
                form(novalidate class="user-form")
                    | Name &nbsp;
                    input(type="text" ng-model="user.name")
                    | <br /><br />
                    | Email &nbsp;
                    input(type="text" ng-model="user.email")
                    | <br /><br />
                    button(ng-click="reset()") Reset
                    button(ng-click="update(user)") Save
                    | <br /><br />
    .row
        .col-md-12
            pre
                | form = {{ user | json }}
            pre
                | master = {{ master | json }}
    script. 
        function Controller($scope) {
            $scope.master = {};

            $scope.update = function(user) {
                $scope.master = angular.copy(user);
            };

            $scope.reset = function() {
                $scope.user = angular.copy($scope.master);
            };

            $scope.reset();
        }

The problem was the pre elemtents were scoped out of the div containing the (ng-controller= "controller") . I was able to solve the problem by restructuring my view like this:

extends layout

block content

    .row(ng-controller="Controller")
        .col-md-12
            div
                form(novalidate class="user-form")
                    | Name &nbsp;
                    input(type="text" ng-model="user.name")
                    | <br /><br />
                    | Email &nbsp;
                    input(type="text" ng-model="user.email")
                    | <br /><br />
                    button(ng-click="reset()") Reset
                    button(ng-click="update(user)") Save
                    | <br /><br />
        .col-md-12
            pre
                | form = {{ user | json }}
            pre
                | master = {{ master | json }}
    script. 
        function Controller($scope) {
            $scope.master = {};

            $scope.update = function(user) {
                $scope.master = angular.copy(user);
            };

            $scope.reset = function() {
                $scope.user = angular.copy($scope.master);
            };

            $scope.reset();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM