从视图中修改ng模型

Modify ng-model from view

本文关键字:ng 模型 修改 视图      更新时间:2023-09-26

我是AngularJS的新手,如何从我的视图中更新我的ng模型的值?现在我有表单,正在更新app_key输入,但当我调用模块并检查$scope.variable时,值没有改变。

这是我的代码

  <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>

在我的模块中,我有选择用户的方法

  $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)">

我使用您的代码实现了这个工作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>