简体   繁体   English

TypeError:无法设置未定义的属性“

[英]TypeError: Cannot set property '' of undefined

For some reason, I can't seem to bring this variable into scope to be manipulated. 由于某些原因,我似乎无法将此变量带入要操作的范围。

Markup: 标记:

<label>Description only <input ng-model="filter.Description"></label><br>
<label>Tags only <input ng-model="filter.Tags.Text"></label><br> //This methodology works

<div ng-repeat="t in filteredTags" style="display:inline-block">
    <button class="btn btn-warning" ng-click="FilterByTag(t.Text)">
        {{t.Text}}
    </button>
</div> //But I want these buttons to work for the same thing, and they don't

..... .....

<tr ng-repeat="b in filteredBookmarks| filter: filter:strict">

JavaScript: JavaScript:

 $scope.FilterByTag = function (tagName) {
        filter.Tags.Text = tagName;
    };

If I change the JS variable to $scope.filter.Tags.Text = tagName; 如果我将JS变量更改为$scope.filter.Tags.Text = tagName; I get an error that reads: TypeError: Cannot set property 'Text' of undefined . 我收到一条错误消息: TypeError: Cannot set property 'Text' of undefined Has anybody run into something similar? 有人碰过类似的东西吗?

Try initializing your filter object first: 尝试先初始化您的过滤器对象:

$scope.filter = {
    'Tags': {
        'Text': null
    },
    'Description': null
}

Your function should read: 您的函数应为:

$scope.FilterByTag = function (tagName) {
    $scope.filter.Tags.Text = tagName;
};

Your filter object is on the $scope 您的filter对象在$scope

Of course it is because the hierarchy you are specifying for setting your tagName is not present. 当然,这是因为指定的用于设置tagName的层次结构不存在。 so it find the Tags itself is undefined and you are trying to set the Text value of it which is undefined and you got the error. 因此它发现标签本身是未定义的,并且您尝试设置未定义的标签的Text值,并且出现了错误。

Try to specify a default data structure of your object where you want to store the value. 尝试指定要在其中存储值的对象的默认数据结构。

$scope.filter = {
  'Tags': {
      'Text': undefined
  },
  'Description': undefined
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM