2016-04-20 161 views
-1

我通過我的控制器這樣一個Ajax請求設置文本框的值後得到的文本框的值公司文本框中的公司名稱。但是,當對該文本框進行更改並按下保存按鈕時,文本框的新值不會顯示 - 它仍會獲得原始文本。這是在控制器按鈕點擊:AnjularJS按下按鈕

$scope.saveChanges=function(){ 

      alert($scope.company); 

    }; 

爲什麼$scope.company不是文本框的新價值?難道我做錯了什麼? (抱歉,如果基本我是新來的角)

+0

嘗試通過公司向的SaveChanges功能 – sebenalern

+0

這是一個與30個左右的字段的表格,我真的需要把他們全都傳下去 –

+0

nvm哈哈你可以添加更多的代碼,所以我可以看看? – sebenalern

回答

2

嘗試刪除value=""

HTML:

<label class="item item-input item-stacked-label"> 
    <span class="input-label">Company :</span> 
    <input type="text" placeholder="Company" ng-model="company"> 
</label> 

<button class="button button-positive" style="margin-top:10px;" ng-click="saveChanges()"> 
    Save Changes 
</button> 

在相應的控制器:

$scope.saveChanges = function() { 
    alert($scope.company); 
}; 
+0

仍然不是新的價值:( –

+1

你可以有一個基本的小提琴,讓我可以在那裏調試它? – softvar

0

你不必爲觀察者模型。

$scope.$watch('company', function(company) { 
    console.log(company); 
} 

或者在你的模板,你可以插值值:

{{company}} 

然後你就可以看到它改變HTML頁面上,因爲角度使觀看者對於它的幕後

0

需要把this.company並不是$範圍

0

請定義$ scope.company前$ HTTP調用

app.controller('myCtrl', function($scope, $http){ 
    $scope.company = null; // <-- 
    $http({method: 'GET', url: url}).success(function(data) { 
    $scope.valzz = data; 
    $scope.company = $scope.valzz[0].Company; 

    }); 
} 

因爲一旦部分已經渲染,它只獲得$scope中定義的屬性。之後,如果任何財產被添加到$範圍內,它需要調用$apply

希望這可以幫助你。

0

我假設你的$ scope工作正常,並且你已經在你的應用中初始化了控制器,並且鏈接到了正確的控制器文件。請將您的代碼與我在此處的代碼進行比較,因爲這樣可以正常工作。

HTML:

<html> 
    <head> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.5/angular.min.js"></script> 
    <script src ="controller.js"></script> 
    </head> 
<body> 

<div ng-app="myApp" ng-controller="testController"> 
<form> 
    <label class="item item-input item-stacked-label"> 
       <span class="input-label">Company :</span> 
       <input type="text" placeholder="Company" ng-model="company"> 
       </label> 

    <button class="button button-positive" style="margin-top:10px;" ng-click="saveChanges()"> 
     Save Changes 
    </button> 
</form> 
</div> 
</body> 
</html> 

控制器:

(function(){ 
    'use strict'; 
    var app = angular.module('myApp', []); 
    app.controller('testController', [ '$scope', function ($scope){ 

      $scope.saveChanges=function(){ 
       alert($scope.company); 
      }; 

     }]); 

})(); 

編輯:Fiddle