在NG-重复AngularJS NG-模型$范围是未定义 [英] AngularJS ng-model $scope in ng-repeat is undefined

查看:193
本文介绍了在NG-重复AngularJS NG-模型$范围是未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提前道歉,如果我不是这个写法正确。我有一个文本框 NG-模型内的 NG-重复,当我试图让文本框的值它总是未定义。我只是希望它显示不管我在对应的文本框中键入。

I apologize in advance if i'm not wording this properly. I have a textbox with ng-model inside an ng-repeat and when I try to get the textbox value it's always undefined. I just want it to display whatever I type in the corresponding textbox.

这似乎是与 $范围的一个问题,所以我将如何让 $ scope.postText 全球或在控制器根级别,因此它可以被访问?

It seems to be an issue with the $scope, so how would I make the $scope.postText global or at the controller root level so it can be accessible?

这里的的jsfiddle帮助明确的事情了: http://jsfiddle.net/stevenng/9mx9B/14/

Here's the JSFiddle to help clear things up: http://jsfiddle.net/stevenng/9mx9B/14/

推荐答案

在您点击前pression可以参考在你的<$的 postText 和访问C $ C> savePost 功能。如果这不是在NG-重复,你可以访问单 $ scope.postText ,但成功的ng-repeat 会为每个项目的新范围。

In your click expression you can reference the postText and access it in your savePost function. If this wasn't in an ng-repeat you could access the single $scope.postText successfully but ng-repeat creates a new scope for each item.

这里是一个更新的小提琴。

Here is an updated fiddle.

<div ng-repeat="post in posts">
   <strong>{{post}}</strong>
   <input type="text" ng-model="postText">
   <a href="#" ng-click="savePost(postText)">save post</a>
</div>

$scope.savePost = function(post){
   alert('post stuff in textbox: ' + post);
}

这篇关于在NG-重复AngularJS NG-模型$范围是未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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