2016-06-09 47 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 ={}; 
    ... 
}