2017-10-14 116 views
0

我的組件代碼:角度成分的雙向綁定工作不

(function() { 
    'use strict'; 

    function SubscribeController(toaster, EmbHTTPFactory) { 
    var ctrl = this; 

    ctrl.submit = function() { 
     EmbHTTPFactory.subscribeToNewsletter(ctrl.email).then(function (res) { 
     toaster.pop('success', "User successfully subscribed"); 
     },function (err) { 

     }); 
    } 
    } 

    var app = angular.module('24hourdigitizing'), 
     config = { 
     templateUrl: "app/views/components/form-component.html", 
     controller: ['toaster', 'ng24hdDigitizing.factories.EmbHTTPFactory', SubscribeController], 
     bindings: { 
      label: '<', 
      button: '<', 
      action: '<', 
      classname: '<' 
     } 
     }; 

    app.component('formComponent', config); 

}()); 

HTML模板

<div class="container"> 
     <div class="row"> 
      <form action="" name="formElement" class="form-element"> 
       <div class="form-group no-margin"> 
        <label for="email" ng-if="$ctrl.label">{{ $ctrl.label }}</label> 
        <div class=""> 
         <input class="form-control input-lg" 
           type="text" 
           id="email" 
           name="email" 
           ng-modal="$ctrl.email" 
           placeholder="Email..." 
           ng-required="true" 
         > 
        </div> 
        <button 
          type="button" 
          class="btn btn-primary btn-lg" 
          ng-disabled="formElement.$invalid" 
          ng-click="$ctrl.submit()" 
        >{{ $ctrl.button }}</button> 
       </div> 
      </form> 
     </div> 
    </div> 

問題: 據我所知$ ctrl.email應該是一個雙向綁定變量。但是,無論何時我提交表單,我都沒有定義。 在代碼中ctrl.email是未定義的。

任何人都可以請解釋一下,如果我在這件事上做錯了什麼?

回答

1

有錯字,它應該是

ng-model="$ctrl.email" 

,而不是ng-modal