这个问题是我最近在项目中碰到的,暂时没找到原因,找到一个解决方法,还多请大神指教,在Stack Overflow找到解决方法:

I am having some "problems" reading an ng-model from the controller.

I want to do this:

<input type="text" ng-model="code">
<button ng-click="setCode()">Login</button>

And in my controller access that variable like this:

$scope.setCode = function(){
alert($scope.code);
}

That is what I want to do but my code variable is undefined.

I found 2 ways to get this to work:

1) Passing the variable as an argument

<input type="text" ng-model="code">
<button ng-click="setCode(code)">Login</button>

and:

$scope.setCode = function(code){
alert(code);
}

2) Declaring my variable as an object

<input type="text" ng-model="code.text">
<button ng-click="setCode()">Login</button>

and:

$scope.code = {text: 'foo'};

[...]

$scope.setCode = function(){
console.log($scope.code);
}

(I prefer the second one)

But I am a little confused about what I should do. It's ok to declare my ng-models like objects? I have to declare ALL my models like objects?

EDIT: Here is a Plnkr of the problem

In this example (I am using the Ionic framework), I declare a variable code with the value test, when I change the model in the input and press the Start button, in theory I have to see in an alert the new value of the model. But what I see is the old one.

Thanks!

解决:

I fixed your plnkr example using object model instead of primitive type.

$scope.model = {};
$scope.model.code = "test"; $scope.setCode = function(){
alert($scope.model.code);
}

转:http://stackoverflow.com/questions/22762725/ng-model-undefined-in-the-controller

Ng-model undefined in the controller的更多相关文章

  1. MVC(Model(模型) View(视图) Controller(控制器))

    复习 1.      商品表 增删改查 index.php  add.php   view.php   edit.php   action.php 2.      MVC(Model(模型)  Vie ...

  2. ASP.NET MVC轻教程 Step By Step 4——Model、View和Controller

    ASP.NET MVC中的Model(数据模型)主要包括定义数据结构.数据库读写.数据验证等等和对象处理相关的工作. 在解决方案资源管理器中找到Model文件夹,点击右键,添加一个新类,名为“Mess ...

  3. Asp.Net Core 入门(四)—— Model、View、Controller

    和我们学习Asp.Net MVC一样,Asp.Net Core MVC的Model.View.Controller也和我们熟悉的Asp.Net MVC中的相似.不同的是我们在使用Asp.Net Cor ...

  4. 2013/11/22工作随笔-缓存是放在Model层还是放在Controller层

    web网站的典型代码框架就是MVC架构,Model层负责数据获取,Controller层负责逻辑控制,View层则负责展示. 一般数据获取是去mysql中获取数据 但是这里有个问题,我们不会每次请求都 ...

  5. 1、ASP.NET Core2.0之Model、View、Controller

    一.新建空项目 打开VS2017,新建→项目,选择如下: 点击,确定,弹出的界面选择如下: 选择空项目,因为选择其他的话会自动生成很多用不到的类,显得项目不够“清爽”,ASP.NET Core选择2. ...

  6. Controller将Model数据传给View层,View层应该如何处理?

    首先,我们在Model层中添加一个Person类. namespace MVCTest.Models{    public class Person    {        public string ...

  7. Model View Controller (MVC) Overview

    By Rakesh Chavda on Jul 01, 2015 What is MVC?Model View Controller is a type of user interface archi ...

  8. angularJS 系列(五)--controller AS 语法

    原文: http://www.cnblogs.com/whitewolf/p/3493362.html 这篇国外的文章也非常好: http://codetunnel.io/angularjs-cont ...

  9. ASP.NET Core 中文文档 第二章 指南(4.2)添加 Controller

    原文:Adding a controller 翻译:娄宇(Lyrics) 校对:刘怡(AlexLEWIS).何镇汐.夏申斌.孟帅洋(书缘) Model-View-Controller (MVC) 架构 ...

随机推荐

  1. MVC DisplayTemplates and EdiotrTemplates.

    我们在mvc项目里经常要对枚举,日期,副文本输入,我们可以用笨拙的方法去view页面里绑定呈现的html内容,而且这种办法不能重用,也就是在不同的view里还是需要做相同的事情,给个日期空间选择例子吧 ...

  2. C#中泛型默认关键字(default)详解

    我们在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 T:(T 是引用类型还是值类型?)对此我们将如何处理? C#代码实例: /// <summary&g ...

  3. spark介绍

    什么是Spark Spark是UC Berkeley AMP lab所开源的类Hadoop MapReduce的通用的并行计算框架,Spark基于map reduce算法实现的分布式计算,拥有Hado ...

  4. hdu_5776_sum(前缀和维护)

    题目链接:hdu_5776_sum 题意: 给你一串数,问你是否有一个连续的子序列的和为m的倍数 题解: 维护一个前缀和%m的值,如果前缀和%m的值为0或者有两个前缀和%m的值相同,那么就有一个连续区 ...

  5. linuxmint卸载软件

    删除你已经卸载掉的软件包的命令为 sudo apt-get autoclean 还有一类软件包,我们每个人都应该删除,那就是你已经卸载了,但是一些只有它依赖而别的软件包都不需要的软件包还留在你的系统里 ...

  6. Redis如何保存数组和对象

    个人建议使用PHP自带的序列化函数serialize和unserialize函数 我们可以封装一个自己的Redis类 <?php class MyRedis{ private static $h ...

  7. qmake pro工程设置

    ----我的生活,我的点点滴滴!! #以下是在terminal里面编译,想编译哪个就修改pro文件 #================================================= ...

  8. 图解如何 将Excel里的数据导入到sql server数据库中

    项目中,经常会碰到如何将Excel里的数据导入到sql server中的问题. 下面,图解如何实现导入Excel中的数据到sql server 2008 R2: Excel截图如下: 查询pub数据库 ...

  9. 扩展方法where方法查询不到数据,不会抛异常,也不是返回的null

    如题,“扩展方法where方法查询不到数据,不会抛异常,也不是返回的null”,示例代码如下: Product类: public class Product { private string name ...

  10. git 提交到github时不用每次都输入用户名,密码

    Permanently authenticating with Git repositories, Run following command to enable credential caching ...