ng-repeat 中的 AngularJS ng-model $scope 未定义 [英] AngularJS ng-model $scope in ng-repeat is undefined

查看:30
本文介绍了ng-repeat 中的 AngularJS ng-model $scope 未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的措辞不正确,我提前道歉.我在 ng-repeat 中有一个带有 ng-model 的文本框,当我尝试获取文本框值时,它总是 undefined.我只希望它显示我在相应文本框中键入的任何内容.

这似乎是 $scope 的问题,那么我如何将 $scope.postText 设为全局或在控制器根级别使其可以访问?

这是帮助解决问题的 JSFiddle:http://jsfiddle.net/stevenng/9mx9B/14/

解决方案

在您的点击表达式中,您可以引用 postText 并在您的 savePost 函数中访问它.如果这不在 ng-repeat 中,您可以成功访问单个 $scope.postTextng-repeat 为每个项目创建一个新的范围.

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

<strong>{{post}}</strong><input type="text" ng-model="postText"><a href="#" ng-click="savePost(postText)">保存帖子</a>

$scope.savePost = 函数(后){alert('在文本框中发布内容:' + post);}

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.

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?

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

解决方案

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-repeat 中的 AngularJS ng-model $scope 未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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