我对 Angular js 的看法如下,
<label class="control-label">Name:</label>
<input type="text" class="form-control" ng-model="config.name" />
<br />
<label class="control-label">versionSpecificApiConfig:</label>
<input type="text" class="form-control" ng-model="config.versionSpecificApiConfig" />
<br />
<label class="control-label">schemaUrl:</label>
<input type="text" class="form-control" ng-model="config.versionSpecificApiConfig.schemaUrl" />
<br />
我这样做是因为我需要如下的配置对象结构,
{
"name":"string",
"versionSpecificApiConfig":{
"string":{
"schemaUrl":"string"
}
}
}
但收到错误“无法在 versionSpecificApiConfig 字段中设置字符串属性”。请帮助我创建这样的结构。(结构中的“字符串”指的是特定文本框的相应值)。
最佳答案
你想要这样的东西吗?
var app= angular.module("app" , []);
app.controller('Ctrl', ['$scope', Ctrl]);
function Ctrl($scope){
$scope.config = {};
$scope.Print = function(){
console.log($scope.config);
}
};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div ng-controller="Ctrl" ng-app="app">
<label class="control-label">Name:</label>
<input type="text" class="form-control" ng-model="config.name" />
<br />
<label class="control-label">versionSpecificApiConfig:</label>
<input type="text" class="form-control" ng-model="config.versionSpecificApiConfig.string.schemaUrl" />
<br />
<label class="control-label">schemaUrl:</label>
<input type="text" class="form-control" ng-model="config.versionSpecificApiConfig.string.schemaUrl1" />
<br />
<button type="button" ng-click="Print()">Click to see object</button>
<br/>
{{config}}
</div>
关于javascript - 在angularjs中为文本字段字符串创建动态对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43315651/