当我预期未定义

时间:2016-02-10 20:43:15

标签: angularjs scope

我有以下Angular代码:

的index.html

<body>
    <main ng-app="app">
        <div class="container">
            <div class="parent" ng-controller="ParentController">
                <p>{{person.name}}</p>
                <p>{{person.email}}</p>
                <div class="child" ng-controller="ChildController">
                    <div class="">
                        <p><input type="text" ng-model="email"></p>
                        <button ng-click="updateEmail(email)" name="button">Submit Email</button>
                    </div>
                    <p>{{person.name}}</p>
                    <p>{{person.email}}</p>
                </div>
            </div>
        </div>
    </main>

    <script src="scripts\lib\angular.min.js" charset="utf-8"></script>
    <script src="scripts\controllers\ParentController.js" charset="utf-8"></script>
</body>

和app.js

var app = angular.module("app", []);

app.controller("ParentController", ParentController);
app.controller("ChildController", ChildController);

ParentController.$inject = ["$scope"];
ChildController.$inject = ["$scope"];

function ParentController($scope) {
    $scope.person = {
        name: "Name"
};
}

function ChildController($scope) {
    $scope.person.email = "Not the real email";
    $scope.updateEmail = function(theEmail) {
        $scope.person.email = theEmail;
    };
}

我希望看到&#34; undefined&#34;在ParentController和ChildController之外的{{person.email}}的页面上进行了dipslayed,但这不是发生了什么。加载页面后,我可以在ParentController和ChildController中看到我的占位符值。如果尚未在对象上设置属性,那么ParentController如何在ChildController中查看该组的值。我知道ChildController应该能够在ParentController中看到所有内容,因为继承,这正是我测试的内容。但为什么ParentController可以看到person.email的价值?

0 个答案:

没有答案