2017-04-09 1 views
0

ユーザーに提示されるフォームを表示するためにモーダルを使用しています。フォームは、私は、モーダルにモーダルに提示した後に戻るコンポーネントののOnSave/onCancelイベントによって返されるパラメータを渡す形式のタグを渡すのOnSaveとonCancel方法AngularJSイベントを渡さないコンポーネントの動的コンパイル

bindings: { 
     entity: '<', 
     readOnly: '<', 
     onSave: '&', 
     onCancel: '&' 
    }, 

ですべてのコンポーネントでありますそれを呼び出し側に返すモーダルです。これを行うには、私は、コンポーネントのプロパティを設定し、それを生成するために、$のコンパイル方法でそれを実行ディレクティブを入れています:

function link(scope, elem, attrs) { 
     if (scope.formType != null && scope.formType != '') { 
      var objString = "<" + scope.formType + " "; //create beginning tag 
      objString += "entity='assignedEntity' read-only='readOnly' "; 
      objString += "on-save='onSave(entity)' on-cancel='onCancel(entity)'>"; 
      objString += "</" + scope.formType + ">"; //add end tag 

      var obj = $compile(objString)(scope); 

      elem.append(obj); 
     } 
    }; 

    return { 
     restrict: 'E', 
     scope: { 
      formType: '=', 
      onSave: '&', 
      onCancel: '&', 
      assignedEntity: '<', 
      readOnly: '<' 
     }, 
     link: link 
    } 

私は、ディレクティブを呼び出すと、一般的なモーダルから適切なプロパティを渡しますボックスのように:

これは、正常に指定されたフォームコンポーネントを生成し、各プロパティの正しい値をフォームコンポーネントに渡します。私の問題は、onSaveまたはonCancelイベントがコンポーネントによってスローされたときに、モーダルコントローラがイベントを受け取っていることです(vm.okまたはvm.cancelが呼び出されます)が、これらのイベントに渡されるパラメータは呼び出しに渡されません。 vm.okとvm.cancelに渡されるプロパティは常に定義されていません。

コンポーネントから、私はこのようなのOnSave/onCancelメソッドを呼び出しています:

ctrl.onSave({ 
      entity: ctrl.entity 
     }); 

と私はctrl.entityはそれで価値を持って実際にはないことを確認しました。

なぜ、パラメータがモーダルコントローラに到達するまでにコールツリーを戻したのかは不明です。

私は私が午前問題定義を支援するために、このplunkrを作成:あなただけリッスン機能の一部としての実体を添付するのを忘れて同じようにそれをデバッグするのビットはそうした後、コードを確認してくださいExample

+0

別段の定めがない限り、コンポーネントは、 '$のctrl'、ない' ctrl'に自分のコントローラをバインドします。詳細については、[AngularJS開発者ガイド - コンポーネント - 比較表](https://docs.angularjs.org/guide/component#comparison-between-directive-definition-and-component-definition)を参照してください。 – georgeawg

+0

うん。私はテンプレート側で$ ctrlを使用します。私はコンポーネントに関連付けられたコントローラでvar ctrl = thisを割り当てます。 ctrl.entityは正しい値(テンプレートから渡された値)を持ち、ctrl.onSaveは実際にそのメソッドに関連付けられた関数を呼び出します。オブジェクト{entity:ctrl.entity}を親コントローラまで渡さないだけです。 – JakeHova

+0

コードに起因する問題について質問するときに、人々が問題を再現するために使用できるコードを提供すれば、より良い回答が得られます。 [最小限で完全で検証可能なサンプルの作成方法](http://stackoverflow.com/help/mcve)を参照してください。 – georgeawg

答えて

1

をクリック$イベントのために。ここには作業plunkerがあります。

(function() { 

    var directiveID = "formGenerator"; 

    angular.module('app').directive(directiveID, ['$compile', FormGenerator]); 

    function FormGenerator($compile) { 

    function link(scope, elem, attrs) { 
     console.log(scope, elem, attrs); 
     if (scope.formType !== null && scope.formType !== '') { 
     var objString = "<" + scope.formType + " "; //create beginning tag 
     //PLEASE TAKE A LOOK HERE, WE'RE EXPECTING THE EVENT TO PROPOGATE TO THE PARENT CONTROLLER 
     //so we take into account the event on-save, the same would have to be done for on-cancel 
     objString += "on-save='onFormSave($event)' on-cancel='onFormCancel(entity)'>"; 
     objString += "</" + scope.formType + ">"; //add end tag 

     var obj = $compile(objString)(scope); 

     elem.append(obj); 
     } 
    } 

    return { 
     restrict: 'E', 
     scope: { 
     formType: '=', 
     onFormSave: '&', 
     onFormCancel: '&' 
     }, 
     link: link 
    } 
    } 
})(); 




(function() { 
    var componentID = "testForm"; 

    var app = angular.module("app"); 

    function TestFormController() { 
    var ctrl = this; 

    ctrl.entity = { 
     name: "this is the entity passed up" 
    }; 

    ctrl.save = function(event) { 
     console.log(event); 
     console.log("In component: " + ctrl.entity.name); 
     ctrl.onSave({ 
     //AND ON SAVE, we make the this controllers model-properties which you'd like to pass on a part of the event. 
     $event: { 
      entity: ctrl.entity 
     } 
     }); 
    }; 

    ctrl.cancel = function() { 
     ctrl.onCancel({ 
     entity: ctrl.entity 
     }); 
    }; 
    } 

    app.component(componentID, { 
    bindings: { 
     onSave: '&', 
     onCancel: '&' 
    }, 
    //Here also we pass the $event to function 
    template: '<h1> This is a test</h1><button type="button" ng-click="$ctrl.save($event);">Save</button>', 
    controller: TestFormController 
    }) 

}()); 
+1

一言、それはそれでした。私はこのために$イベントが渡ったことを完全に忘れていました。ありがとうございました。 – JakeHova

関連する問題