在指令之间共享数据

时间:2014-06-17 18:39:55

标签: angularjs angularjs-directive angularjs-scope angularjs-service mean-stack

我正在渲染json文件中描述的大型表单,在我看来我正在使用ng-repeat,然后将每个元素发送到名为FormRender的'master'指令,我有一个控制器在此指令中,正在启用obj.QuestionType。然后我希望能够根据其类型将该对象发送到不同的指令,例如,如果它是一个多项选择问题,我希望能够将其发送到multipleRender然后加载正确的部分查看里面的数据。

到目前为止,我还不完全确定如何做到这一点,问题以下列格式描述。

{
  "QuestionID": 1,
  "QuestionPage": 1,
  "QuestionName": "CurrentFirms",
  "QuestionType": "text",
  "QuestionLabel": "Current Firm(s)",
},

我正在渲染它

 <form action="" method="POST">
      <fieldset ng-repeat='item in data[0]'>
        <form-render obj='item'></form-render>

formRender指令看起来像这样

.directive('formRender', function() {
  return {
    restrict: 'E',
    scope: { obj: '=' },
    controller: function($scope) {
      var type = $scope.obj.QuestionType;
      switch(type) {
        case 'multiple':
            // send this to a different directive to handle and render
          break;
      }
    }
  };

前进的最佳方式是什么?

干杯,

1 个答案:

答案 0 :(得分:1)

我会使用ngSwitch - AngularJS ngSwitch documentation - 并在模板中选择指令。

<form action="" method="POST">
   <fieldset ng-repeat='item in data[0]' ng-switch on="item.QuestionType">
      <my-form-text-directive ng-switch-when="text" obj="item"></my-form-text-directive>
      <my-form-multiple-directive ng-switch-when="multiple" obj="item"></my-form-multiple-directive>
   <fieldset>
</form>
相关问题