2016-01-02 52 views
0

我使用这个价值NG-模型不

<textarea class="notes" placeholder="Placeholder Text" style="height: 630px;" ng-model="notes"></textarea> 

和更新从我可以理解NG-模型=“笔记”是指textarea的分配无论$ scope.notes是。这工作正常,但是每当我编辑textarea中的文本,是不是$ scope.notes应该改变呢?

当我使用这个按钮:

<button ng-click="saveNotes(notes)"></button> 

的“音符”值始终是原来的$ scope.notes值,而不是更新的价值。

有人可以告诉我这是为什么吗?

感谢

编辑,包括HTML代码:

<ion-view ng-controller="LectureCtrl" id="userMessagesView" view-title="{{lecture.title}}"> 
    <ion-content> 
    <div style="position: absolute; top: 20px; left: 30px; right: 60px;"> 
     <div style="height: 100%;"> 
     <iframe class="video" width="560" height="315" src="{{lecture.youtubeLink}}" frameborder="0" allowfullscreen></iframe> 

     <textarea class="notes" placeholder="Placeholder Text" style="height: 630px;" ng-model="notes" ng-change="updatedNotes"></textarea> 
     </div> 
    </div> 
    </ion-content> 

    <button ng-click="saveNotes(notes)"> 
    </button> 
<ion/view 
+4

总是......总是......总是在'ng-model'中使用一个对象。如果您的视图中有子范围,则绑定将使用基元破坏 – charlietfl

+1

您的代码应该按照书面形式工作。看到这个JSFiddle:https://jsfiddle.net/sscovil/5udnmtgy/ - 你最可能遇到的问题是你的按钮在不同的范围。 –

+0

@ShaunScovil嗯,这是如何工作的?只有一个控制器分配给整个视图不在吗? – tryingtolearn

回答

3

NG-模型视为一种到控制器的变量连接到您的HTML,反之亦然。因此,如果您在控制器中设置了$ scope.notes,然后在您的html中使用了大括号{{notes}},那么变量内容将显示在您的html中,例如

Controller =>(bound to)=> your HTML($ scope.notes)

但它可以双向运行(双向绑定)。因此,如果您在HTML中使用ng-model,它现在将从输入字段获取该内容,并将其设置为控制器中的变量。你不必做任何事情,因为它是在Angular的背景下完成的,即如果你在你的文本区域输入“Hello world”,它已经更新到控制器中的变量。所以你不需要把它传回去。

控制器< =(结合到)< = HTML(NG-模型= “笔记”)

2路结合有3份(非常简化):

  1. 变量被设置用控制器中的$ scope。 $ scope.notes声明 控制器“伞”内的任何代码或HTML将具有 访问该变量。
  2. 使用ng-model将$ scope变量连接到HTML元素。超过 简化的ng-model只是说将$ scope.notes变量连接到 这个元素。我认为它是作为你的HTML和ctrl 之间的管道,并且你不需要触摸它,因为可变内容正在流动 由Angular管理的两个之间
  3. 使用{{}}解析(绑定)变成了UI,即 {{注意事项}}说表明变量
  4. 内容

约束力的2WAY的简单的例子

<input type="text" ng-model="first.greeting"> 
    <div ng-class="first.greeting"> 
    {{first.greeting}} {{"World"}} 

    </div> 

代码 //没有EED通过票据支持它已经存在于你的CTRL

// proof 
    .controller('MyController', ['$scope', function($scope) { 
     // if you set this up the string wills how up in your textarea 
     $scope.notes = "Set notes two way binding"; 

     // click on notes and it will loot whatever you have entered into your input field 
     $scope.saveNotes = function() { 
      console.log('Lets check if notes is here', $scope.notes); 
     } 

    }]) 

    // Note if you set $scope.notes = "Set notes two way binding"; in your ctrl the "Set notes two way binding" wills how up in your textbox 
    <textarea class="notes" placeholder="Placeholder Text" style="height: 630px;" ng-model="notes"></textarea> 

“伞” $范围的问题 - $范围没有连接到它的控制器

现在,如果这已经是有道理的,你和你拥有这一切的设置,你仍然遇到问题,那么它很可能是你有$范围问题,即“管道”到控制器断开。我认为这是从一个地区代码移动到另一个区域代码,即每天拨打555,然后您飞到另一个州,然后尝试使用555,但由于地区代码是777,因此不起作用。$ scope.555只能使用一个知道约555的控制器。如果你是一个不同的状态(控制器),555意味着什么,在这种情况下Angular将“丢弃它”。它实际上并没有丢弃它只是假设你是聪明的,拨打其他地方,它目前还不知道(如国际),所以它并将它传递假设系统中的一些地方会知道怎样用555

示例“断开”范围

.controller('ProductsController', ['$scope', function($scope) { 
     $scope.product = "My product"; 

     $scope.saveProduct = function() { 
      console.log('Lets check if product is here', $scope.product); 
     } 
    }]) 

    .controller('ReviewsController', ['$scope', function($scope) { 

    }]) 

    <div ng-controller="ProductsController"> 
     /// lots of html stuff 
    </div> 
    <div ng-controller="ReviewsController"> 
      // Note: ProductsController is no longer managing the product variable. OtherStuffController now has "control" of all variables and it has no product variable 
      // this seems logical but it happens all the time especially with large HTML or in my case template hell :) There are a number of tools to help with this debugging 
      <textarea class="notes" placeholder="Placeholder Text" style="height: 630px;" ng-model="product"></textarea> 
    </div> 
+0

感谢您花时间和精力为我做出更清晰的表达。最后的问题是“离子含量指令有一个孤立的范围”。 – tryingtolearn

+0

大声笑,是的,这将做到这一点 – Nolan