AngularJS:模型不在控制器范围内更新

时间:2015-06-13 12:17:01

标签: javascript angularjs

我有以下控制器:

app.controller('MyCtrl', function($interval, $scope) {
    $scope.foo = 2;
    $interval(function() {
        console.log($scope.foo);
    }, 1000);
});

我认为以下代码:

<input type="text" ng-model="foo" />

当我加载页面时,输入正确填充了值&#34; 2&#34;。但是,如果我更改输入中的值,控制台将继续记录&#34; 2&#34; (没有引号)。

我使用$interval只是为了说明 - $watch()回调只会触发一次,然后再也不会再触发。如果我在输入上使用ng-change="",则回调中的$scope.foo始终等于2.

我做错了什么?

1 个答案:

答案 0 :(得分:12)

如果您使用ng-model,则必须在there中添加一个点。

通过创建像这样的对象来绑定模型

<强>控制器

<input type="text" ng-model="form.foo" />

查看

Imports System.Xml
Module Module1

    Const FILENAME As String = "c:\temp\test.xml"
    Sub Main()
        Dim doc As New XmlDocument
        doc.Load(FILENAME)
        Dim names As XmlNodeList = doc.GetElementsByTagName("Name")

        Dim diseases As New List(Of Disease)
        For Each name As XmlNode In names
            Dim newDisease As New Disease
            diseases.Add(newDisease)

            newDisease.id = name.Attributes("id").Value
            newDisease.text = name.InnerText
            newDisease.subArticle = name.SelectSingleNode("SubArticle").InnerText
            newDisease.mainArticle = name.SelectSingleNode("MainArticle").InnerText
            newDisease.image = name.SelectSingleNode("Image").InnerText


        Next

    End Sub

    '  <Name id="1">
    '  Info1
    '  <SubArticle>Info1</SubArticle>
    '  <MainArticle>Info1</MainArticle>
    '  <Image>Info1</Image>
    '</Name>

End Module
Public Class Disease
    Public id As Integer
    Public text As String
    Public subArticle As String
    Public mainArticle As String
    Public image As String
End Class
​