无法读取属性' firstName'未定义的

时间:2016-06-20 14:01:10

标签: angularjs

我收到错误,"无法读取属性",如果我按照以下方法操作。我想知道如何解析角度JS。是在执行自定义Javascript之前还是之后完成的。

 //Angular JS code

            myApp = angular.module('myApp', []);

            myApp.controller('controller', function($scope)
            {
                    $scope.student = {
                    firstName: "Mark",
                    lastName: "Smith",

                    //returns a string

                    dummyVar: (function () {
                        return "Hello";
                    }()),

                    fullName: (function() {
                        var studentObject;
                        studentObject = $scope.student;
                        return studentObject.firstName + " " + studentObject.lastName;
                    }())
                };
            });

    //Html Code

    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.js"></script>
    </head>
    <body ng-app="myApp">
        <div ng-controller="controller">
            First name is {{ student.firstName }}
            <br><br>
            Last name is {{ student.lastName }}
            <br><br>
            Dummy Variable is {{ student.dummyVar }}
            <br><br>
            Full Name is {{ student.fullname }}
        </div>

        <script type="text/javascript" src="function.js"></script>
    </body>
    </html>**

1 个答案:

答案 0 :(得分:0)

在$ scope对象中注册函数fullName时,尚未创建对象本身。所以$ scope.student不存在,并且在创建对象时设置为undefined;

在创建对象后使用过滤器或函数,或使API返回对象连接全名。