无法在angularjs中读取null的属性$ scope.dropdownfield [英] Cannot read property $scope.dropdownfield of null in angularjs

查看:72
本文介绍了无法在angularjs中读取null的属性$ scope.dropdownfield的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的注册表页面.表单页面具有person_id字段和下拉列表名称condition_code_1.Person_id字段是必填字段,而condition_code_1下拉列表是可选字段,表示是否选择用户是可选字段.下拉列表的值是通过specialConds数组获取的,数据是从oracle db字段获取的.

I have a simple registration form page. The form page have a person_id field and drop down list name condition_code_1. Person_id field is mandatory field meanwhile the condition_code_1 drop down list is an optional field mean's that it is optional for user to be selected or not. The value of the drop down list is fetched through specialConds array that data are get from oracle db field.

我将下拉列表的值存储在Condition_code_1控制器的数据数组中.问题是当我不选择列表并提交表单时,angularjs抛出错误:无法读取null的'condCode1'属性.未选择下拉列表时,我该如何忽略它.这里有什么帮助.

I stored the value of drop down list in data array in Condition_code_1 controller.The problem is when i do not select the list and submitted the form, angularjs throws an error : Cannot read property 'condCode1' of null. How do i ignored the drop down list when it is not selected. Any help here.

1)Condition_code_1列表

  <select id = "condition_code_1"
          data-placeholder = "Please select"   
          ng-model         = "specialCond.condCode1"
          ng-options       = "t.condCode as t.condDesc for t in specialConds" chosen>
          <option value="">&nbsp;</option>
  </select>

2)Condition_code_1控制器

 $scope.create = function () {
    var data = {};

    data.personId = $scope.person_id;
    data.conditionCode1 = $scope.specialCond.condCode1; 

    CondCodeSvc.create(data, function(res){
           jAlert('Create successfull');
    }, errSvc.errorHandler);                   
 };

推荐答案

这是因为在调用$ scope.create()之前, data 不存在,因此未定义,这意味着不是对象,这意味着它不能具有属性,因此javascript会引发错误.

That's because data doesn't exist before you call $scope.create(), therefore is undefined, which means is not an object, which means it cannot have a property, thus javascript throws the error.

尝试:

var data = {};
$scope.create = function () {

    data.personId = $scope.person_id;
    data.conditionCode1 = $scope.specialCond.condCode1; 

    CondCodeSvc.create(data, function(res){
       jAlert('Create successfull');
    }, errSvc.errorHandler);                   
};

这篇关于无法在angularjs中读取null的属性$ scope.dropdownfield的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆