programing

"controller as" 구문으로 명명된 양식을 참조하는 방법

codeshow 2023. 10. 4. 23:04
반응형

"controller as" 구문으로 명명된 양식을 참조하는 방법

angularjs에서 "controller as" 구문을 사용할 때 컨트롤러의 명명된 양식을 참조하는 데 문제가 있습니다.예를 들어, 다음과 같은 HTML이 주어졌을 때:

<div ng-controller="MyController as ctl">
  <form role="form" name="newItemForm">
    <input type="text" id="firstName" ng-model="ctl.firstName"/>
  </form>
</div>

콘트롤러의 상황에서,

function MyController() {
  var self = this;
  console.log(self.newItemForm);
}

self.newItemForm정의되지 않았습니다.만약 제가 $scope 협약을 사용하고 있었다면 $scope.newItemForm을 참조할 수 있었을 것입니다.스코프를 사용하지 않고 구문으로 컨트롤러에서 이 작업을 수행할 수 있는 다른 방법이 있습니까?

HTML을 다음으로 변경합니다.

<div ng-controller="MyController as ctl">
  <form role="form" name="ctl.newItemForm">
    <input type="text" id="firstName" ng-model="ctl.firstName"/>
  </form>
</div>

그러면 주입하지 않고도 컨트롤러에서 예상대로 명명된 양식에 액세스할 수 있습니다.$scope. 여기에서 이 정보를 찾으십시오: http://www.technofattie.com/2014/07/01/using-angular-forms-with-controller-as-syntax.html

언급URL : https://stackoverflow.com/questions/22539475/how-to-refer-to-named-forms-with-controller-as-syntax

반응형