简体   繁体   English

如何在Angular工厂内定义ngModel?

[英]How to define ngModel inside angular factory?

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. 在这里tranobj和field将被动态获取。 The problem is I'm defining rowset as an array but it's an object inside the property '0'. 问题是我将行集定义为数组,但这是属性“ 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; 
}]);

You have to map your object to an array of objects. 您必须将对象映射到对象数组。 You can go through this link Converting a JS object to an array . 您可以通过此链接将JS对象转换为数组

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;

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

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