Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript TypeError:无法读取属性';fname';未定义的_Javascript_Html_Css_Angularjs - Fatal编程技术网

Javascript TypeError:无法读取属性';fname';未定义的

Javascript TypeError:无法读取属性';fname';未定义的,javascript,html,css,angularjs,Javascript,Html,Css,Angularjs,这是HTML表单。我需要在单击“编辑”按钮时将条目放回表单。它指向脚本函数selectEdit() 添加成员 不是有效的电子邮件地址 通讯录 名称 电子邮件 联系电话 地址 选择权 {x.fname+“”+x.lname} {{x.email} {{x.contact}} {x.addres=$index} | 问题是我无法将条目放回表单。It错误无法读取未定义的属性“fname” <script type="text/javascript">

这是HTML表单。我需要在单击“编辑”按钮时将条目放回表单。它指向脚本函数selectEdit()






添加成员


不是有效的电子邮件地址

通讯录


名称 电子邮件 联系电话 地址 选择权 {x.fname+“”+x.lname} {{x.email} {{x.contact}} {x.addres=$index} |
问题是我无法将条目放回表单。It错误无法读取未定义的属性“fname”

<script type="text/javascript">
        var app = angular.module("MyApp", []);

        app.controller("mainController", function($scope) {

            $scope.x = [];
            $scope.address= [];
            $scope.fname = "";
            $scope.lname = "";
            $scope.email = "";
            $scope.contact = "";
            $scope.addres = "";

            $scope.addItem = function() {
                $scope.address.push({
                    fname: $scope.fname,
                    lname: $scope.lname,
                    email: $scope.email,
                    contact: $scope.contact,
                    addres: $scope.addres
                });

            $scope.fname = "";
            $scope.lname = "";
            $scope.email = "";
            $scope.contact = "";
            $scope.addres = "";

            };

            $scope.remove = function(index) {
                var isConfirmed = confirm("Are you sure to delete this record?");

                if(isConfirmed) {
                    $scope.address.splice(index,1);

                }
                else {
                    return false;
                }

            };

            $scope.selectEdit = function(index) {
                console.log(index);
                var chenes = getSelectedIndex(index);
                var list = $scope.address[chenes];
                $scope.fname = list.fname;
                $scope.lname = list.lname;
                $scope.email = list.email;
                $scope.contact = list.contact;
                $scope.addres = list.addres;


            };

            function getSelectedIndex(index){
            for(var i=0; i<$scope.address.length; i++)
                if($scope.address[i].index==index)
                    return i;
                return -1;  
        };

        });



    </script>

var-app=angular.module(“MyApp”,[]);
应用控制器(“主控制器”,功能($scope){
$scope.x=[];
$scope.address=[];
$scope.fname=“”;
$scope.lname=“”;
$scope.email=“”;
$scope.contact=“”;
$scope.addres=“”;
$scope.addItem=函数(){
$scope.address.push({
fname:$scope.fname,
lname:$scope.lname,
电子邮件:$scope.email,
联系人:$scope.contact,
地址:$scope.addres
});
$scope.fname=“”;
$scope.lname=“”;
$scope.email=“”;
$scope.contact=“”;
$scope.addres=“”;
};
$scope.remove=函数(索引){
var isConfirmed=confirm(“您确定要删除此记录吗?”);
如果(未确认){
$scope.address.splice(索引1);
}
否则{
返回false;
}
};
$scope.selectEdit=函数(索引){
控制台日志(索引);
var chenes=getSelectedIndex(索引);
var list=$scope.address[chenes];
$scope.fname=list.fname;
$scope.lname=list.lname;
$scope.email=list.email;
$scope.contact=list.contact;
$scope.addres=list.addres;
};
函数getSelectedIndex(索引){

对于(var i=0;i而言,地址数组似乎不包含
chenes
, 尝试在这一行之前对chenes和地址执行console.log:
var list=$scope.address[chenes];


我猜属性索引不存在于地址项中,并且chenes的值为-1,这就是为什么
list
未定义(导致错误的原因)

您的错误来自哪一行,是来自$scope还是list?在脚本中,选择编辑按钮并指向$scope.selectEdit=function(索引)在执行以下操作后列表未定义:var list=$scope.address[chenes];列表是否具有名为fname的属性?在分配它之后立即添加console.log(list),以查看它是否真的是您想要的。
<script type="text/javascript">
        var app = angular.module("MyApp", []);

        app.controller("mainController", function($scope) {

            $scope.x = [];
            $scope.address= [];
            $scope.fname = "";
            $scope.lname = "";
            $scope.email = "";
            $scope.contact = "";
            $scope.addres = "";

            $scope.addItem = function() {
                $scope.address.push({
                    fname: $scope.fname,
                    lname: $scope.lname,
                    email: $scope.email,
                    contact: $scope.contact,
                    addres: $scope.addres
                });

            $scope.fname = "";
            $scope.lname = "";
            $scope.email = "";
            $scope.contact = "";
            $scope.addres = "";

            };

            $scope.remove = function(index) {
                var isConfirmed = confirm("Are you sure to delete this record?");

                if(isConfirmed) {
                    $scope.address.splice(index,1);

                }
                else {
                    return false;
                }

            };

            $scope.selectEdit = function(index) {
                console.log(index);
                var chenes = getSelectedIndex(index);
                var list = $scope.address[chenes];
                $scope.fname = list.fname;
                $scope.lname = list.lname;
                $scope.email = list.email;
                $scope.contact = list.contact;
                $scope.addres = list.addres;


            };

            function getSelectedIndex(index){
            for(var i=0; i<$scope.address.length; i++)
                if($scope.address[i].index==index)
                    return i;
                return -1;  
        };

        });



    </script>