简体   繁体   English

如何将新的键/值对元素添加到现有数组?

[英]How to add new key/value pair element to an existing array?

var onHomePageLoaded = function(retMsg)
 {
  $scope.data = retMsg.data.records;
  $scope.data.link : 'http://www.newwebsite.com'

 }

After i have added link element (key/value) to the javascript object, i am not able to get the same in the HTML template 将链接元素(键/值)添加到javascript对象后,我无法在HTML模板中获得相同的内容

<div ng-repeat="record in data">

  <a ng-href="{{record.link}}"> Click Here </a>

</div>

Javascript is a dynamic language. Javascript是一种动态语言。 You can add properties to existing objects in a very simple way , like assigning a value to an existing property. 您可以通过非常简单的方法向现有对象添加属性,例如为现有属性分配值。 Just add a new property 只需添加一个新属性

$scope.data.link = 'http://www.newwebsite.com'

if retMsg.data.records is an array, still you can add a property to $scope.data . 如果retMsg.data.records是一个数组,仍然可以向$scope.data添加一个属性。

if you want different link for every object in array then, do this. 如果要为数组中的每个对象使用不同的链接,请执行此操作。

$scope.data.forEach(function(obj){
    obj.link = "your custom link" // write your logic here to produce different link.
});

If data is an array you can use 如果数据是数组,则可以使用

$scope.data.push(yourData);

for example 例如

$scope.data.push({link : 'http://www.newwebsite.com'});

Or if you want to access the objects inside the array and add them a key value pair you can do as follow: 或者,如果要访问数组内部的对象并为它们添加一个键值对,则可以执行以下操作:

// add the link to the first entry
$scope.data[0].link = 'http://www.newwebsite.com';

Sorry. 抱歉。 Do not know if I understood well. 不知道我是否很好理解。

Maybe you can define scope.data as: 也许您可以将scope.data定义为:

$scope.data = {retMsg.data.records}

Then for example a function: 然后例如一个函数:

    $scope.addNew = funtion(){
     $scope.data.newElement = $scope.viewElement
    };

In your HTML 在您的HTML中

  <label>{{data}}</label> // Which makes reference to the $scope.data at the controller 
  <input ng-change="addNew()" ng-model="viewElement"></input>
  <label>{{data.newElement}} // Will be empty at the very beginning but will show the new element once it is created.

Hope it helps 希望能帮助到你

I see several issues with your code. 我发现您的代码有几个问题。

First, you use the variable name record in your ng-repeat , but then use report in ng-href . 首先,在ng-repeat使用变量名称record ,然后在ng-href使用report I assume those should be the same. 我认为这些应该是相同的。

Also, link isn't a member of record , it is a member of data . 另外, link不是record的成员,而是data的成员。 You set it as a member of data here: $scope.data.link : 'http://www.newwebsite.com' . 您可以在此处将其设置为data成员: $scope.data.link : 'http://www.newwebsite.com' If you want to add that link to each record , in your onHomePageLoaded function, you'll need to loop through all the records you add to data , and add the link property to each one. 如果要将链接添加到每个record ,请在onHomePageLoaded函数中,遍历添加到data所有记录,并将link属性添加到每个记录。

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

相关问题 如何向现有数组状态变量添加新的key:value对 - How to add a new key:value pair to an already existing array state variable react native 将新的键值对添加到现有的 Firebase - Add new key value pair to existing Firebase 将键/值对添加到现有对象数组 - Add key/value pair to existing array of objects 如何在javascript中向数组添加新对象(键值对)? - How to add a new object (key-value pair) to an array in javascript? 将键、值对添加到现有数组中,并有条件设置键的格式 - Add Key, Value pair to existing array with a condition to format the Key 如何在现有的JavaScript对象中添加新的key:value对? - How do I add a new key:value pair in existing JavaScript object? 如何在循环内动态地向现有的 Javascript 对象添加新的键:值对 - How can I add a new key:value pair to an existing Javascript object dynamically within a loop 如何将新的键值对添加到 mongoDB 文档的现有子对象 - How to add a new key:value pair to existing sub object of a mongoDB document 如何使用JavaScript在现有JSON对象中添加新的键值对? - How to add a new key value pair in existing JSON object using JavaScript? 如何基于另一个键值将新的键值对添加到数组中的对象 - How to add new key value pair to an object in an array based on another key-value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM