簡體   English   中英

ng-model在angular js中不起作用

[英]ng-model is not working in angular js

我從api生成html,但無法從數據綁定中獲取其值。 的HTML

<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);
    }
});

當我點擊提交按鈕時,我變得不確定。 請幫助我要去哪里錯了。

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM