2016-06-09 68 views
0

我從api生成html,但我無法從數據綁定中獲取它的值。 HTMLng-model在角js中不工作

<div class="content-fluid ng-scope" id="angular_template"> 
     <div class="row"> 
      <div class="col-md-6"> 
       <label class="col-md-12">First Name</label> 
       <input type="text" ng-model="frm1.first_name" class="textboxStyle col-md-6 form-control" placeholder="first name"> 
      </div> 
      <div class="col-md-6"> 
       <label class="col-md-12">Last Name</label> 
       <input type="text" ng-model="frm1.last_name" class="textboxStyle col-md-6 form-control" placeholder="last name"> 
      </div> 
     </div> 
    </div> 
<input type="button" ng-click="save(frm1)" value="Submit" /> 

控制器:

mod.controller('CreateSimpleFormController', function($scope, $http) { 
    $http.get('<url>').success(function(response) { 
    var angularTemplate = response;// html string 
      document.getElementById("angular_template").innerHTML = angularTemplate; 
    }).error(function(error) { 
     console.log('error', error); 
    }).finally(function() {}); 

    $scope.save = function(data){ 
     console.log(data); 
     console.log($scope.frm1.first_name); 
    } 
}); 

我得到未定義當我點擊提交按鈕。請幫助我出錯的地方。

+0

什麼是未定義的? – adolfosrs

+0

您沒有提交表單,但是您在代碼中使用了'type =「submit」'。嘗試使用類型按鈕的按鈕,看看它是否工作。 –

+0

嘗試填充兩個輸入,然後按提交 – SuperComupter

回答

0

$ scope.frm1是你必須在控制器中定義的東西。如果您的html輸出有任何文本,並且您期望將其綁定到模型變量,則不會發生這種情況。 Angular評估模板,發現它沒有定義變量,因此會使其變爲空白(忽略當前文本值)

+0

我是新角js。我只想點擊提交按鈕上的名字和姓氏值。我也嘗試過你提到過的,但沒有給出價值。 – user2114575

+0

好的,正如其他人指出的那樣,只需在你的控制器中使用'$ scope.frm1 = {};'作爲第一行 – insanevirus

0

如果要使用點(。)值,則必須在使用它之前聲明它。

mod.controller('CreateSimpleFormController', function($scope, $http) { 
    $scope.frm1 ={}; 
    ... 
}