0

ng-model="name")でスコープにアクセスする必要があります。 DynamicFormCtrlの形式でデータ入力にアクセスするにはどうすればよいですか?私はそれらにアクセスする方法を知りません。自分のスコープでデータにアクセスすることはできますが。あなたが構文としてコントローラを使用しているようはDynamicFormCtrl</em><em></strong><strong>親コントローラで、私は、フォームデータにアクセスする必要がNG-includeディレクティブ

<body ng-app="exampleApp" ng-controller="DynamicFormCtrl as ctrl"> 
     <div> 

    <button ng-click="ctrl.addForm(0)">Form One</button> //add form1 
    <button ng-click="ctrl.addForm(1)">Form Two</button> //add form 2 
    <button ng-click="ctrl.addForm(2)">Form Three</button>// add form 3 
    </div> 


    <div ng-repeat="form in ctrl.displayedForms track by $index"> //display form on button press 



    <ng-include src="form"></ng-include> //Include from from the Script 
    </div> 

    <script type="text/ng-template" id="form1.tpl.html"> 
    <label> 
    {{name}} 
     Form one is an input: <input type="text" ng-model="name"/> 
    </label> 
    </script> 

    <script type="text/ng-template" id="form2.tpl.html"> 
    <label> 
     Form two gives you choices: <input type="radio"/> <input type="radio"/> 
    </label> 
    </script> 

    <script type="text/ng-template" id="form3.tpl.html"> 
    <button>Form three is a button</button> 
    </script> 
    <script type="text/javascript"> 
    angular.module("exampleApp", []) 
    .controller("DynamicFormCtrl", function($scope) { 
    var ctrl = this; 
    $scope.form_name = $scope.name; 
    console.log($scope.form_name); 

    var forms = [ 
     "form1.tpl.html", 
     "form2.tpl.html", 
     "form3.tpl.html", 
    ]; 

    ctrl.displayedForms = []; 

    ctrl.addForm = function(formIndex) { 
     ctrl.displayedForms.push(forms[formIndex]); 
    } 
    }); 
    </script> 
</body> 
+0

をお勧めコントローラ –

+0

の$ scope.name =「あなたの名前」を追加、さらに読書のために

@MarcusHねえ、form1.tpl.html内のローカルスコープに動作しますが、コントローラ上にない –

+0

この質問を理解していない。 DynamicFormCtrlの親コントローラはありますか? DynamicFormCtrlから親スコープにアクセスしたいのですか? –

答えて

1

あなたはその後

をctrl.nameを用いて入力を使用することができ、コントローラは、入力フィールド

<input type="text" ng-model="ctrl.name"/> 

でctrl.nameを使用する必要が

また、controller-as構文を使用してスコープを挿入する必要もありません。コントローラ-ようにする私は https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md

関連する問題