如何解决TypeError:无法将未定义或null转换为Function.keys(<anonymous>)中的对象

时间:2016-11-10 12:46:04

标签: javascript angularjs

我试图隐藏这个领域。当用户在glyphicon-eye-open上进行cliks时,我将显示glyphicon-eye-close并将条件视为true。如果条件为真,那么我将该值推入数组。

我正在使用以下功能,我有两个角色演员和歌手。 两者都是具有相同型号名称的相似字段。我根据角色显示值。

我能够将值推送到actor的数组中。但如果对歌手尝试相同,则显示以下错误。

即使我尝试使用不同的型号名称,但我仍然遇到同样的错误。

 TypeError: Cannot convert undefined or null to object
        at Function.keys (<anonymous>)
        at $$childScopeClass.ManageProfileController.$scope.pushrolefield (manageprofile.controller.js:214)
        at angular.js:10773
        at angular.js:18981
        at $$childScopeClass.$eval (angular.js:12608)
        at $$childScopeClass.$apply (angular.js:12706)
        at HTMLLabelElement.<anonymous> (angular.js:18980)
        at HTMLLabelElement.dispatch (jquery-2.0.3.min.js:5)
        at HTMLLabelElement.y.handle (jquery-2.0.3.min.js:5)

我的HTML

<div class="wrap">
<div class="panel-body newPanelBody" ng-if="displaySingerInfo && !displayActorInfo">
 <div class="newWrap">
            <label for="qual"  class="first-heading">About You*<span class="styleatr">not more than 160 Characters</span></label> 
            <textarea rows="4" cols="10" id="qual" class="textarea1" placeholder="say something about you " ng-model="model.aboutyou">
            </textarea> 

        </div>
                 <label ng-class="{'glyphicon glyphicon-eye-close': allowrole.saboutyou, 'glyphicon glyphicon-eye-open': !allowrole.saboutyou}" ng-click="pushrolefield(allowrole.saboutyou = allowrole.saboutyou?false:true)"><input class="eyecheck1" type="checkbox" ng-model="allowrole.saboutyou" ng-hide="allowrole.saboutyou || !allowrole.saboutyou"/></label>             
                            </div>
</div>

<div class="panel-body newPanelBody" ng-if="displayActorInfo && !displaySingerInfo">
 <div class="newWrap">
            <label for="qual"  class="first-heading">About You*<span class="styleatr">not more than 160 Characters</span></label> 
            <textarea rows="4" cols="10" id="qual" class="textarea1" placeholder="say something about you " ng-model="model.aboutyou">
            </textarea> 

        </div>
                 <label ng-class="{'glyphicon glyphicon-eye-close': allowrole.saboutyou, 'glyphicon glyphicon-eye-open': !allowrole.saboutyou}" ng-click="pushrolefield(allowrole.saboutyou = allowrole.saboutyou?false:true)"><input class="eyecheck1" type="checkbox" ng-model="allowrole.saboutyou" ng-hide="allowrole.saboutyou || !allowrole.saboutyou"/></label>             
                            </div>
</div>
</div>

我的控制器:

$scope.rolehide = {
        aboutyou: 'aboutyou',
        saboutyou: 'saboutyou
    };

    $scope.allowrole = {
        aboutyou: false,
        saboutyou: false
    };

    $scope.rolehiddenfields = [];

    $scope.pushrolefield = function(allow){
        debugger;
        console.log(allow);
        $scope.rolehiddenfields = [];
        var users = {}, 
            allows = $scope.allowrole;
      Object.keys(allows).forEach(function(key){
        allows[key] ? users[key] = $scope.rolehide[key] : null;
      });
      $scope.rolehiddenfields.push(users);
    }; 

1 个答案:

答案 0 :(得分:0)

在JS文件末尾附近使用单行if条件,为null。用空对象或空数组替换null。