吴模型不更新控制器值 [英] Ng-model does not update controller value

查看:132
本文介绍了吴模型不更新控制器值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许愚蠢的问题,但我有我的简单的输入和按钮的HTML表单:

Probably silly question, but I have my html form with simple input and button:

<input type="text" ng-model="searchText" />
<button ng-click="check()">Check!</button>
{{ searchText }}

然后在控制器(模板和控制器从routeProvider称呼):

Then in the controller (template and controller are called from routeProvider):

$scope.check = function () {
    console.log($scope.searchText);
}

为什么我看到在控制台中查看点击该按钮时,正确的,但不确定的更新?

Why do I see the view updated correctly but undefined in the console when clicking the button?

谢谢!

更新:
好像我其实已经解决了这个问题(必须想出一些解决方法之前)有:
只有改变从 SEARCHTEXT 我的属性名称 search.text ,然后定义空 $ scope.search = {}; 控制器,瞧对象......不知道为什么它的工作,虽然]

Update: Seems like I have actually solved that issue (before had to come up with some workarounds) with: Only had to change my property name from searchText to search.text, then define empty $scope.search = {}; object in the controller and voila... Have no idea why it's working though ;]

推荐答案

在您的标记试试这个

<input type="text" ng-model="searchText" />
<button ng-click="check(searchText)">Check!</button>
{{ searchText }}

这在您的控制器

$scope.check = function (searchText) {
    console.log(searchText);
}

这篇关于吴模型不更新控制器值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆