0

I have a scope inside a directive. I am able to access the directive scope inside my factory but after getting the scope I need to bind it with input controls for a two way binding.

For example the directive scope: This scope getting from controller and then assigning to directive.

 $scope.model = {
    "entityinfo": {
      "entity":"",
      "tenantId":"292FEC76-5F1C-486F-85A5-09D88096F098",
      "timeStamp":"2015-12-15T10:16:06.322Z"
    },
    "collections":{}
 }

I need to bind this scope to an input control

var template='<input  ng-model="model[tranobj].rowset[0][field]">';

Here tranobj and field will be fetched dynamically. The problem is I'm defining rowset as an array but it's an object inside the property '0'.

It's coming in like this:

"Customer29Jan16": {
      "rowset": {
        "0": {
          "CuId": "dgdfg",
          "Name": "dgdgf",
          "Quantity": "3",
          "Rate": "5",
          "Amount": "4"
        }
      }
    }

But I need it like this:

"Customer29Jan16": {
      "rowset": [
         {
          "CuId": "dgdfg",
          "Name": "dgdgf",
          "Quantity": "3",
          "Rate": "5",
          "Amount": "4"
        }
        ]
    }

Directive Code added:

bosAppModule.directive('layoutTableCellControlControlRender',['$compile','layoutRenderingControlsFactory','soObjectFactory','$parse', function($compile,layoutRenderingControlsFactory,soObjectFactory,$parse){
    var layoutTableCellControlRenderObj={};
    linkFnTableCellControlRender=function(scope, element, attributes, controllerCtrl) {
        scope.controlData="NOCONTROLDATA";
        var controlContent="";
        var bindingPath = "";
        angular.forEach(scope.mapperData.collections.controltype.rowset, function (value, index) {
            if(value.controltypeid==scope.controlId){
                scope.controlData=value.controltypename;
                if(scope.controlData){
                    controlContent=angular.element(layoutRenderingControlsFactory[scope.controlData](scope.controlId, bindingPath));
                    $compile(controlContent)(scope);
                    element.append(controlContent);
                }else{
                    console.log("## control data not matching");
                }

            }
        }); 

    };  
    layoutTableCellControlRenderObj.scope={attributeId:'=',controlId:'=',layoutData:'=',pageObject:'=',mapperData:'=', cellControlId:'=', soData:"=",model:"=",field:"@",tranobj:"@" };
    layoutTableCellControlRenderObj.restrict='AE';
    layoutTableCellControlRenderObj.replace='true';
    layoutTableCellControlRenderObj.template="<div class='col-xs-12 col-sm-12 col-md-6 col-lg-6' ng-attr-id={{cellControlId}} cell-control-id='tablecellcontrol.layouttablecellcontrolcellcontrolid' " +
                                            "control-id='tablecellcontrol.layouttablecellcontrolcontroltypeid' " +
                                            "layout-data='layoutData' page-object='pageObject' mapper-data='mapperData' attribute-id='tablecellcontrol.layouttablecellcontrolbindingobjectattributeid'" +
                                            "model='model' field={{tablecellcontrol.attributename}} tranobj={{tablecellcontrol.objectname}}>" +

        layoutTableCellControlRenderObj.link = linkFnTableCellControlRender;
    return layoutTableCellControlRenderObj; 
}]);
bagya
  • 383
  • 1
  • 11
  • 38
  • 1
    Could you post a punker snippet? Also you should not access whole scope from factory. – Ankit Pundhir Apr 29 '16 at 07:04
  • Where do you get the data from? Can't you modify it so it looks the way you want it to? – Patrick Apr 29 '16 at 07:11
  • @Patrick..I will get the data from directive and directive will get data from controller. – bagya Apr 29 '16 at 07:15
  • @AnkitPundhir . I will try to create plunker.please give some time, because lengthy coding. – bagya Apr 29 '16 at 07:16
  • But if *you* are creating the data, why are you not defining it as an array and use push, instead of some sort of `rowset['0'] = value`? – Patrick Apr 29 '16 at 07:19
  • Can you show the code where you push data to the Customer29Jan16.rowset "array"? – Patrick Apr 29 '16 at 07:20
  • @Patrick - Directive code added. Please have a look. – bagya Apr 29 '16 at 07:25
  • Where you have bind your link function with `layoutTableCellControlRenderObj` ? – Ankit Pundhir Apr 29 '16 at 07:27
  • Consider your design pattern, I can't think of any situation where you HAVE TO bind view inputs directly to factory objects. this is why controllers are there – Kobi Cohen Apr 29 '16 at 07:33
  • @KobiCohen - Actually i forgot mention the $scope.model getting from controller only then i am binding to directive then from directive to factory. – bagya Apr 29 '16 at 07:36
  • @AnkitPundhir - layoutTableCellControlRenderObj is an object to create the inout controls. – bagya Apr 29 '16 at 07:47
  • Yeah but `linkFnTableCellControlRender` is just floating around in code. Where you have assigned it to your `layoutTableCellControlRenderObj` object. – Ankit Pundhir Apr 29 '16 at 07:53
  • @AnkitPundhir Sorry please see edited directirve code now. By mistake i removed link function also – bagya Apr 29 '16 at 07:56

1 Answers1

1

You have to map your object to an array of objects. You can go through this link Converting a JS object to an array .

You can do it like this inside controller

var array = [];
angular.forEach(Customer29Jan16.rowset, function(object, index) {
    array.push(object);
});

console.log(array);
Customer29Jan16.rowset = array;
Community
  • 1
  • 1
Ankit Pundhir
  • 1,097
  • 8
  • 13
  • Yeah..you're correct but how i can create array inside object either in directive or ng-model. Please help in my case... – bagya Apr 29 '16 at 09:23
  • What do mean by array inside object either in directive or ng-model? Will you please elaborate. – Ankit Pundhir Apr 29 '16 at 11:43
  • If i will achieve above angular for each loop inside controller. how two binding will work? that is the reason i asked, how i will code inside directive or ngModel to achieve two way binding. – bagya Apr 29 '16 at 13:32