2014-07-23 2 views
7

나는이 $ 범위에서 간단한 console.log()을 만들려고 노력 해요 : 이것은 내 자바 스크립트 파일이

<div ng-controller="CustomerController" id="customer-block"> 

    <h3>Customer Information</h3> 

    <div class="col-md-4"> 
     <label>Address 1:</label> 
     <input type="text" ng-model="customer.address1" class="form-content" 
     id="customer-address1" /> 
    </div> 

    <div class="col-md-4"> 
     <label>Address 2:</label> 
     <input type="text" ng-model="customer.address2" class="form-content" 
     id="customer-address2" /> 
    </div> 

    <div class="col-md-4"> 
     <label>City</label> 
     <input type="text" ng-model="customer.city" class="form-content" 
     id="customer-city" /> 
    </div> 

</div> 

입니다

:

lima3app.controller("CustomerController", function($scope){ 

    console.log($scope.customer); 

}); 

그러나 로그에 이 정의되지 않은으로 반환됩니다. 그게 뭐가 잘못 됐니? 그 시간에 $의 scope.customer에서의 CustomerController 컨트롤러를 초기화하기 때 값이없는 그것은 정의되지 않은 반환 http://plnkr.co/edit/MU2i46o03bs22Jwh6QIe

+1

'$ scope.customer'는 어디에서도 초기화되지 않으므로 정의되지 않아야합니다. 이러한 입력에 내용을 입력하면 데이터가 시작됩니다. – runTarm

답변

5

다른 사람들이 말했듯이 고객 개체를 초기화해야합니다.

고객이 컨트롤러에서 설정 한 값이 없으므로보기에 정의되지 않은 것으로 표시됩니다. 당신이 입력 상자에 값을 입력하면이 더 이상 정의되지 않은 수 없습니다 만, 로깅부터 입력 상자에 영향 여기

Plunker Demo

을 값이 입력되지 한번 처음에만 수행됩니다 내가 변경 한 부분입니다 in script.js

lima3app.controller("CustomerController", function($scope){ 

$scope.customer = { 
    address1 : 'address1', 
    address2 : 'address2', 
    city:'city' 
} 

console.log($scope.customer); 

}); 
0

당신의 JS 코드 때문에

console.log($scope.customer); 

실행 :

은 plunkr입니다.

관련 문제