2016-03-02 63 views
0

我是AngularJS的新手,我如何從我的視圖更新ng-model的值? 現在我有窗體,我正在更新app_key輸入,但是當我調用我的模塊並檢查$ scope.variable時,值沒有改變。從視圖中修改ng-model

這裏我的代碼

<div> 
    <br> 
    <div class="field_row"> 
     <label for="app_key">AppKey:</label> 
     <input id="app_key" type="text" ng-model="appKey"> --> this is the input that I´m changing the value 
    </div> 
</div> 

<div class="modal-footer"> 
    <button type="button" class="btn btn-info" ng-click="selectUser()"> 
    Select 
    </button> 
</div> 

而我的模塊中我有方法selectUser

$scope.selectUser = function() { 
     $scope.setAppKey($scope.appKey); --> Here the appKey it´s not modified 
}; 

解決方案

我不知道爲什麼,但要使它工作,我需要將ng模型傳遞給控制器​​功能

<input id="app_key" type="text" ng-model="appKey" ng-change="changeAppKey(appKey)"> 
+0

如果你定義在'html'控制器? –

+0

是的,它以前定義在html – paul

+0

你在瀏覽器控制檯上有任何錯誤嗎? –

回答

2

我用你的代碼使這個working JSFiddle。它似乎工作得很好。你確定你的setAppKey功能是正確的嗎?

JS

var myApp = angular.module('myApp',[]); 

function MyCtrl($scope) { 
    $scope.selectUser = function() { 
     console.log($scope.appKey); 
    }; 
} 

HTML

<div ng-controller="MyCtrl"> 
    <div> 
     <div class="field_row"> 
      <label for="app_key">AppKey:</label> 
      <input id="app_key" type="text" ng-model="appKey"> 
     </div> 
    </div> 
    {{appKey}} 
    <div class="modal-footer"> 
     <button type="button" class="btn btn-info" ng-click="selectUser()"> 
     Select 
     </button> 
    </div> 
</div> 
+0

它確實工作,我不知道爲什麼不在我的應用程序。但非常感謝!我將使用此代碼作爲比較的基礎 – paul