简体   繁体   English

角度-创建对象图

[英]Angular - Creating an object map

I need to be able to dynamically create parameter objects in Angular which have the following form: 我需要能够在Angular中动态创建具有以下形式的参数对象:

[
    password: {type: 'String', required: true, value: 'apassword'},
    someRandomParam: {type: 'Integer', required: false, value:3}
]

I know how to bind to this in order to display the values in a list or table, but I don't know how to construct this object with bindings in Angular, primarily in creating the key ("password") as these keys are created dynamically too. 我知道如何绑定到此列表以显示列表或表中的值,但是我不知道如何使用Angular中的绑定构造此对象,主要是在创建这些键时创建键(“密码”)也是动态地

I'm assuming that you meant having an object, not an array (your code will generate SyntaxError), like that: 我假设您的意思是拥有一个对象,而不是一个数组(您的代码将生成SyntaxError),如下所示:

$scope.params = {
    password: {type: 'String', required: true, value: 'apassword'},
    someRandomParam: {type: 'Integer', required: false, value:3}
};

It's true that you can bind to it, even using ngRepeat. 的确,即使使用ngRepeat也可以绑定到它。 But adding something to that object needs something more than just a simple bind. 但是,向该对象添加某些东西不仅需要简单的绑定。 Create some inputs in your view: 在您的视图中创建一些输入:

<input type="text" ng-model="newParamName" />
<input type="text" ng-model="newParam.type" />
<input type="checkbox" ng-model="newParam.required" />
<input type="text" ng-model="newParam.value" />
<input type="button" ng-click="addParam(newParamName, newParam)" value="Add me!" />

...and in controller: ...并在控制器中:

$scope.addParam = function(name,param){
    $scope.params[name] = angular.copy(param);
};

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

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