Combine a variable with variable value in Javascri

2019-03-04 09:45发布

I am trying to do something like this in Angular javascript (a simplified code):

var modelName = "date";

if (attrs.hasOwnProperty('today')) {
   scope.modelName = new Date();
}

In the above, I actually want scope.modelName to become scope.date automatically. How can I parse the modelName variable to its value?

1条回答
\"骚年 ilove
2楼-- · 2019-03-04 10:35

You can access properties of objects using square brackets.

var modelName = "date";

if (attrs.hasOwnProperty('today')) {
   scope[modelName] = new Date();
}
查看更多
登录 后发表回答