2013-02-14 2 views
0

angularjs 응용 프로그램다음은

<input type="text" class="input-xlarge" id="user_name" ng-model="myModel.text1" 
    name="user_name" rel="popover" data-content="Enter your first and last name." 
    data-original-title="Full Name"> 

을 내 HTML 코드 그리고 여기에 하나 개의 텍스트 상자에 생성자 코드

function MyCtrl2($scope) { 
     var initial = {text1: 'initial value'}; 
     var ini = {text2: 'initialvalue'}; 
     $scope.myModel = angular.copy(initial); 
     $scope.myModel = angular.copy(ini); 
} 

MyCtrl2.$inject = ['$scope']; 

을 기본값입니다 usr_email이 채워집니다.하지만 아니요. user_name 잘못된 점을 지적 할 수 있습니까?

답변

0

angular.copy은 두 번째로 사용할 때 (init 복사시) $ scope.myModel을 대체하므로 사용할 수 없습니다. 모델에 INI 초기의 모든 속성을 복사하는 대신 angular.extend를 사용하여 약간 짧은 버전은 angular.extend 될

function MyCtrl2($scope) { 
    var initial = {text1: 'initial value'}; 
    var ini = {text2: 'initialvalue'}; 
    $scope.myModel = {}; 
    angular.extend($scope.myModel, initial, ini); 
} 
+0

($ scope.myModel =를 {}, 초기, 초기화). 한 줄 저장;) – matys84pl