[英]AngularJS Push in Collection, Show from Collection, $watch changes
I use object ITEMS to hold data 我使用对象ITEMS来保存数据
$scope.items = [
{value:'title1', text: 'head1',
value:'title2', text: 'head2',
value:'title3', text: 'head3' }
];
When I clicked 'Add option' button I need show 'value' and 'text' in HTML page: 当我单击“添加选项”按钮时,我需要在HTML页面中显示“值”和“文本”:
$scope.items.push(
{
value: 'value1',
text: 'text1'
}
);
I can show object length, but I can't show added option. 我可以显示对象的长度,但不能显示添加的选项。 And $watch ($watchCollection) doesn't work too. 而且$ watch($ watchCollection)也不起作用。
In this example I don't get values from inputs. 在此示例中,我没有从输入中获取值。
There is an issue with how your items array looks at the moment. 目前,您的项数组的外观存在问题。 I think your $scope.items should look like: 我认为您的$ scope.items应该看起来像:
$scope.items = [
{
value: "value1",
text: "text1"
},
{
value: "value2",
text: "text2"
}
]
rather than all in one object, as when you push you'll create a new object. 而不是全部集中在一个对象中,因为当您进行推送时,您将创建一个新对象。
With your question, calling items.value, will result in an undefined. 关于您的问题,调用items.value将导致未定义。
You need to call an object in $scope.items. 您需要在$ scope.items中调用一个对象。 Calling items[$scope.items.length-1] will get the most recent object added, and such items[$scope.items.length-1].value and items[$scope.items.length-1].text the values in that object 调用items [$ scope.items.length-1]将获得最新添加的对象,而此类items [$ scope.items.length-1] .value和items [$ scope.items.length-1] .text该对象中的值
Your $scope.items
array is improperly declared. 您的$scope.items
数组未正确声明。 You need braces around each separate item in the array, like this: 您需要在数组中每个单独的项目周围加上大括号,如下所示:
$scope.items = [
{value:'title1', text: 'head1'},
{value:'title2', text: 'head2'},
{value:'title3', text: 'head3'}
];
Your directive is all kinds of messed up. 您的指令各种各样。 You don't even need to create a new directive if all you want to do is display the items in a list. 如果您只想在列表中显示项目,则甚至不需要创建新指令。 You can just do this: 您可以这样做:
<select ng-model="selectedItem" ng-options="item.text for item in items"></select>
Your textboxes are ok, except for the typo in the ng-model="addoText"
. 除了ng-model="addoText"
的错字,您的文本框还可以。 Your labels below should be bound to the same variables as the textboxes. 您下面的标签应与文本框绑定到相同的变量。
key: {{addVal}} <br>and value: {{addText}}
That will update the labels as you type in the textboxes. 当您在文本框中键入内容时,将更新标签。 If you don't want to update the labels until you add a new item, then bind them to some new variables, like this: 如果您不希望在添加新项之前更新标签,则将它们绑定到一些新变量,如下所示:
key: {{newVal}} <br>and value: {{newText}}
Finally, your add()
function should look like this: 最后,您的add()
函数应如下所示:
$scope.add = function () {
$scope.items.push(
{
value: $scope.addVal,
text: $scope.addText
}
);
$scope.newVal = $scope.addVal;
$scope.newText = $scope.addText;
};
This pushes the new item to the array, and sets the bindings on your labels to the new values. 这会将新项目推送到数组,并将标签上的绑定设置为新值。 You don't need to $watch
anything. 您不需要$watch
任何东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.