Wednesday, 15 May 2013

What's a good way of adding and remove object details to an array when a checkbox is checked or unchecked using AngularJS? -


I have an API that takes objects

  {'title': 'some' , 'Character': 'object', 'environment': '[{id': 1}, {'id': 2}]}  

I have to add new entries There is a form of setting a description for the title and object that I am going to pass my API:

  & lt; Form class = "form horizontal" role = ng-submit = "vm.submitForm ()" & gt; & Lt; Label = "title" & gt; Title & lt; / Label & gt; & Lt; Input class = "form-control" id = "title" type = "text" ng-model = "vm.myObj.title" placeholder = "title" /> & Lt; Label = "desc" & gt; Details & lt; / Label & gt; & Lt; Input class = "form-control" id = "disc" type = "text" ng-model = "vm.myObj.desc" placeholder = "description" /> & Lt; Div class = "checkbox" ng-repat = "env in vm.availableEnvs" & gt; & Lt; Label & gt; & Lt; Input type = "checkbox" /> {{Env.label}} & lt; / Labels & gt; & Lt; / Div & gt; & Lt; Hour / & gt; & Lt; Button & gt; Submit & lt; / Button & gt; & Lt; / Form & gt; In the form you probably noticed that I have a  ng-repeat  which repeats on an array of environmental objects: 

  (id: 2, label: "EU"}, {id: 3, label: "ASIA"}, {id: 4, label: "Africa"}];  

Since there is a need to get an array of APIs: {id: x} objects, I was thinking that there is a good way to add (and delete) anyone

My .js code:

  (function () (onservices), Microsoft. $ Injection = ['Envisivea']; fiction microlton (envisioner) {var vm = this; vm.availableEnvs = EnvService.getAvailableEnvvironments (); vm.myObj = {title: '', desc: '', environment: []}; vm. SubmitForm = submitForm; Function Submit Form () {// Make a service call that handles $ http and creates a POST with my obj)} function anvas () {this.environments = [ID: 1, label: {ID: 2, label: "EU"}, {ID: 3, label: "Asia"}, {ID: 4, label: "Africa"}]; This.getAvailableEnvironment = function () { Return.environments; }}}} ();  

What I do in such cases is as follows:

  • Setting subscriptions, i.e. intermediate array [i] is true if is included in main [i] , an intermediate Create an array of postal data,
  • $ watchCollection on intermediate array and set target target accordingly.
  • FOKE BELD:

    (I have also changed the service to return promise - I believe this is a best practice, but this Not mandatory for answers.)


    No comments:

    Post a Comment