如何在angularjs中动态访问对象?

时间:2021-08-19 00:09:56

I am trying to access the object dynamically based on the ng-click.I will have lot's of form with button like this.

我试图基于ng-click动态访问该对象。我将使用这样的按钮有很多形式。

HTML

HTML

<button type="button" id="news" ng-click="home.addnew()">Update</button>
<button type="button" id="allocation" ng-click="home.addnew()">Update</button>
<button type="button" id="create" ng-click="home.addnew()">Update</button>

My Object

我的对象

"home": {
    "news": [{
        "type": "cd",
        "title": "title1",
    }],
    "allocation": [{
        "type": "cd",
        "title": "title2",
    }],
        "create": [{
        "type": "cd",
        "title": "title3",
    }]
    .......etc
}

Angularjs Controller

Angularjs控制器

$scope.home.addnew = function(){
    var type = $(event.target).attr("id");
       $scope.home.news.title; // Here how can I access that "news","allocation","create" Dynamically
       $scope.home.type.title // I have tried like this 
}

I am getting error

我收到了错误

Cannot read property 'title ' of undefined

3 个解决方案

#1


4  

Your trying to access object property instead of array.

您尝试访问对象属性而不是数组。

It should look like this

它看起来应该是这样的

$scope.home.addnew = function(){
    var type = $(event.target).attr("id");
    $scope.home[type][0].title; 
}

#2


0  

You may use brackets for array

您可以使用括号表示数组

 var dynamicType ='news'; 
                var obj ={"home": {
                "news": [{
                "type": "cd",
                "title": "title1",
                }],
                "allocation": [{
                "type": "cd",
                "title": "title2",
                }],
                "create": [{
                "type": "cd",
                "title": "title3",
                }]
            }};
    console.log(obj.home[dynamicType][0].title); 

#3


0  

Are you sure that you have attached home to $scope as one of the user already commented. $scope.home needs to exist before you start working on it.

你确定你已经作为已经评论过的用户之一附加到$ scope。 $ scope.home需要在开始处理之前存在。

#1


4  

Your trying to access object property instead of array.

您尝试访问对象属性而不是数组。

It should look like this

它看起来应该是这样的

$scope.home.addnew = function(){
    var type = $(event.target).attr("id");
    $scope.home[type][0].title; 
}

#2


0  

You may use brackets for array

您可以使用括号表示数组

 var dynamicType ='news'; 
                var obj ={"home": {
                "news": [{
                "type": "cd",
                "title": "title1",
                }],
                "allocation": [{
                "type": "cd",
                "title": "title2",
                }],
                "create": [{
                "type": "cd",
                "title": "title3",
                }]
            }};
    console.log(obj.home[dynamicType][0].title); 

#3


0  

Are you sure that you have attached home to $scope as one of the user already commented. $scope.home needs to exist before you start working on it.

你确定你已经作为已经评论过的用户之一附加到$ scope。 $ scope.home需要在开始处理之前存在。