且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么我的 ng-model 变量在控制器中未定义?

更新时间:2023-02-12 11:27:51

@DavidTryon 在评论中解决了我的问题.我使用范围错误.我需要使用一个对象而不是一个字符串.换句话说,我需要做一些类似 ng-model="search.term" 而不是 ng-model="search" 的事情,并在 js 中像这样使用它:$scope.search.term.它与继承有关,但长话短说,如果您的 ng-model 中没有点 (.),那么您就做错了.

@DavidTryon solved my issue in the comments. I was using scope wrong. I needed to use an object not a string. In other words I need to do something like ng-model="search.term" not ng-model="search" and use it in the js like this: $scope.search.term. It has to do with inheritance but long story short, if your ng-model doesn't have a dot (.) in it, you're doing it wrong.

这里有更多信息:http://jimhoskins.com/2012/12/14/nested-scopes-in-angularjs.html

谢谢大卫!

汤姆