2014-12-18 10 views
1

$ scope.locationForm에 액세스하려고하면 컨트롤러에서 폼 변수에 액세스 할 수 없습니다 '정의되지 않음'이 있지만 console.log ($ scope) 나는 콘솔에서 loactionForm을 볼 수있다.AngularJS 컨트롤러에서 폼에 액세스 할 수 없습니다

내 HTML 코드

<div ng-controller="LocationsController as ctrl"> 
<form class="form-inline" name="locationForm"> 
    <div class="form-group"> 
     <!-- <div class="input-group"> --> 
      <label for="location-name">Название населенного пункта</label> 
      <input required 
        name="name" 
        ng-model="ctrl.location.name" type="text" class="form-control" id="location-name" placeholder="Название населенного пункта"> 
      <label for="location-name">Район</label> 
      <select required 
        name="region_id" 
        ng-model="ctrl.location.region_id" 
        ng-options="region.id as region.name for region in ctrl.regions" class="form-control" placeholder="Название района"></select> 
      <input ng-click="ctrl.save()" 
        ng-disabled="locationForm.$invalid" type="submit" class="btn btn-default" value="Cохранить"> 
      <a class="btn btn-default" ng-click="ctrl.reset()" ng-show="locationForm.$dirty">Сброс</a> 
     <!-- </div> --> 
    </div> 
</form> 

내 컨트롤러 코드 :

function LocationsController($scope, Location, Region, $q) { 
var lc = this, 
    l_index; 
    lc.form ={}; 
    lc.regions = lc.locations = []; 
    lc.regions = Region.query(); 
    lc.regions.$promise.then(function(data) { 
     lc.locations = Location.query(); 
    }); 
    lc.getRegion = function (id) { 
    return lc.regions.filter(function(obj) { 
     return obj.id == id; 
    })[0].name; 
    }; 
    console.log($scope); 
    // console.log($scope.locationForm); 
    lc.reset = function() { 
     lc.location = new Location; 
    } 
    lc.reset(); 

};

+0

같은 시간 제한을 사용하는 것입니다 –

+0

문제를 해결하는 방법을 알고 계십니까? – huuuk

+0

무엇을 달성하려고합니까 –

답변

1

LocationsController이 초기화되고 form 요소가 아직 컴파일되지 않은 경우 문제가 발생합니다. http://jsfiddle.net/arunpjohny/jcvqdf06/1/ - 그래서 하나의 가능한 해킹 내가 문제가`form` 요소가 컴파일되기 전에`LocationController` 초기화 생각

function LocationsController($scope, Location, Region, $q, $timeout) { 
    //then later 
    $timeout(function(){lc.reset();}) 
} 
관련 문제