javascript - how to dynamically push form input inside object literal with angular -
i'm beginner in angular programming , i'm looking examples onhow interact object literal using language. unfortunately, didn't find examples answer questions topic.so, if have html file :
<body ng-app="watch"> <div ng-controller="reely"> <form> name: <input type="text" ng-model="place.name" ng-change='change()'> <br/> streetadress: <input type="text" ng-model="place.streetadress" ng-change='change()'> <br/> addresslocality : <input type="text" ng-model="place.addresslocality" ng-change='change()'> <br/> addressregion: <input type="text" ng-model="place.addressregion" ng-change='change()'> <br/> postalcode : <input type="text" ng-model="place.postalcode" ng-change='change()'> <br/> addresscountry: <input type="text" ng-model="place.addresscountry" ng-change='change()'> <br/> logo: <input type="text" ng-model="place.logo" ng-change='change()'><br/> url: <input type="text" ng-model="place.url" ng-change='change()'><br/> image:<input type="text" ng-model="place.image" ng-change='change()'><br/> </form> <h1> json </h1> <p> {{mtl}} </p> </div> </body> if user enters in different forms, datas automatically added object literal example :
{ "@context": { "schema": "http://schema.org/" }, "@graph": [ { "@id": "place", "@type": "schema:person", "schema:name": " office", "schema:address": { "@type": "schema.postaladdress", "streetadress": "501 rue william", "addresslocality": "toronto", "addressregion": "on", "postalcode": "h3c 1p4", "addresscountry": "ca" } "schema:logo": "http://active.com/images/logo.png", "schema:url": "http://shop.active.com/products/ra-r436", "schema:image": "http://active.com/images/ra-r4xx.jpg" } } } notice datas should displayed in different places inside literal object.i know that post not clear , hope demo if possible
and controller code :
angular.module("watch", []) .controller("reely", function($scope) { $scope.mtl = { "@context": { "schema": "http://schema.org/" }, "@graph": [ { "@id": "place", "@type": "schema:place", "schema:address": { "@type": "schema.postaladdress" } } ] } $scope.place = {}; $scope.adress= {}; $scope.adress["@type"]= "schema.postaladdress"; function changekeyvalue() { (var key in $scope.place) { if ($scope.place.hasownproperty(key)) { $scope.mtl["@graph"][0]["schema:address"]["schema:" + key] = $scope.place[key]; } } } $scope.change = function () { changekeyvalue(); } });
actually can without effort :) can use notation this:
var obj={ "@graph":[{ "@type":"anyvaluehere" }] }; you can override values this: obj['@graph'][0]['streetaddress']="toronto";
and result, new json object:
{ "@graph":[{ "@type":"anyvaluehere", "streetaddress":"toronto" }] } so based on kind of notation, can this:
this html:
<div ng-controller="reely"> <form> name: <input type="text" ng-model="mtl['@graph'][0]['schema:name']" > <br/> streetadress: <input type="text" ng-model="mtl['@graph'][0]['schema:address'].streetadress" > <br/> addresslocality : <input type="text" ng-model="mtl['@graph'][0]['schema:address'].addresslocality" > <br/> addressregion: <input type="text" ng-model="mtl['@graph'][0]['schema:address'].addressregion" > <br/> postalcode : <input type="text" ng-model="mtl['@graph'][0]['schema:address'].postalcode" > <br/> addresscountry: <input type="text" ng-model="mtl['@graph'][0]['schema:address'].addresscountry" > <br/> logo: <input type="text" ng-model="mtl['@graph'][0]['schema:logo']" ><br/> url: <input type="text" ng-model="mtl['@graph'][0]['schema:url']" ><br/> image:<input type="text" ng-model="mtl['@graph'][0]['schema:image']" ><br/> </form> <h1> json </h1> <p> {{mtl}} </p> </div> this modified controller:
angular.module("watch", []) .controller("reely", function($scope) { $scope.mtl = { "@context": { "schema": "http://schema.org/" }, "@graph": [ { "@id": "place", "@type": "schema:place", "schema:address": { "@type": "schema.postaladdress" } } ] }; }) as can see, use "mtl" object directly html , use object["property"] notation.
so hope helps, , if does, can mark right answer :d
Comments
Post a Comment