可能是愚蠢的问题,但我有我的html表单简单的输入和按钮:

<input type="text" ng-model="searchText" />
<button ng-click="check()">Check!</button>
{{ searchText }}

然后在控制器中(模板和控制器由routeProvider调用):

$scope.check = function () {
    console.log($scope.searchText);
}

为什么我看到视图更新正确,但在控制台未定义时,单击按钮?

谢谢!

更新: 似乎我已经解决了这个问题(之前不得不提出一些变通办法): 只需要把我的属性名从searchText改为search。文本,然后定义空$scope。搜索= {};对象,瞧……但我不知道为什么它能起作用;]


当前回答

I came across the same issue when dealing with a non-trivial view (there are nested scopes). And finally discovered this is a known tricky thing when developing AngularJS application due to the nature of prototype-based inheritance of java-script. AngularJS nested scopes are created through this mechanism. And value created from ng-model is placed in children scope, not saying parent scope (maybe the one injected into controller) won't see the value, the value will also shadow any property with same name defined in parent scope if not use dot to enforce a prototype reference access. For more details, checkout the online video specific to illustrate this issue, http://egghead.io/video/angularjs-the-dot/ and comments following up it.

其他回答

对我来说,这个问题是通过将我的数据存储到一个对象(这里是“数据”)来解决的。

NgApp。controller('MyController',函数($scope) { 美元的范围。My_title = "";//这在ng-click函数中不起作用 美元的范围。数据= { 'my_title': "", }; 美元的范围。doAction = function() { console.log ($ scope.my_title);//错误值 console.log ($ scope.datas.my_title);// Good Value由'ng-model'绑定 } });

我希望它会有帮助

在《用AngularJS掌握Web应用程序开发》一书第19页中写道

避免直接绑定到作用域的属性。双向数据绑定到 对象的属性(在作用域上公开)是首选方法。作为一个 方法提供的表达式中应该有一个点 Ng-model指令(例如,Ng-model ="thing.name")。

作用域只是JavaScript对象,它们模仿dom层次结构。根据JavaScript原型继承,作用域属性是通过作用域分开的。为了避免这种情况,应该使用点符号来绑定ng-models。

由于没有人提到这一点,这个问题可以通过向绑定属性中添加$parent来解决

<div ng-controller="LoginController">
    <input type="text" name="login" class="form-control" ng-model="$parent.ssn" ng-pattern="/\d{6,8}-\d{4}|\d{10,12}/" ng-required="true" />
    <button class="button-big" type="submit" ng-click="BankLogin()" ng-disabled="!bankidForm.login.$valid">Logga in</button>
</div>

还有控制器

app.controller("LoginController", ['$scope', function ($scope) {
    $scope.ssn = '';

    $scope.BankLogin = function () {
        console.log($scope.ssn); // works!
    };
}]);

使用this代替$scope也可以。 函数AppCtrl(美元范围){ 美元的范围。searchText = ""; 美元的范围。Check = function () { console.log(“你输入了”+这个。searchText + "'");//使用'this'而不是$scope } } < script src = " https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js " > < /脚本> < div ng-app > < div ng-controller = " AppCtrl " > <输入ng-model = " searchText " / > <按钮ng-click = "检查()">写入控制台日志</按钮> . < / div > < / div >

编辑:在写这个答案的时候,我的情况比这复杂得多。评论结束后,我试图重现它,以理解它为什么有效,但运气不好。我认为以某种方式(不知道为什么)生成了一个新的子范围,这是指那个范围。但如果使用$scope,它实际上引用的是父$scope,因为javascript的词法范围特性。

如果有人有这个问题,以这种方式测试并通知我们,那就太好了。

I came across the same issue when dealing with a non-trivial view (there are nested scopes). And finally discovered this is a known tricky thing when developing AngularJS application due to the nature of prototype-based inheritance of java-script. AngularJS nested scopes are created through this mechanism. And value created from ng-model is placed in children scope, not saying parent scope (maybe the one injected into controller) won't see the value, the value will also shadow any property with same name defined in parent scope if not use dot to enforce a prototype reference access. For more details, checkout the online video specific to illustrate this issue, http://egghead.io/video/angularjs-the-dot/ and comments following up it.