简体   繁体   English

ng-model值未在控制器中更新:Angular js

[英]ng-model value not updating in controller: Angular js

I used this technique in another place and it worked but in this case it is not working. 我在其他地方使用了此技术,但它确实起作用,但是在这种情况下,它不起作用。 Here lane_title shows undefined. 在这里lane_title显示未定义。 Am I missing something here. 我在这里想念什么吗?

<input type="text" ng-model="lane_title" placeholder="Title of the lane" >
<button ng-click="testScope()">Create</button>

In controller: 在控制器中:

$scope.testScope = function(){
    alert($scope.lane_title);
}

Seems to be working fine. 似乎工作正常。 But make sure to initialize the model variable. 但是请确保初始化模型变量。 Otherwise, it will print as undefined 否则,它将打印为undefined

 angular.module("app",[]) .controller("ctrl",function($scope){ $scope.lane_title = ""; $scope.testScope = function(){ alert($scope.lane_title); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="ctrl"> <input type="text" ng-model="lane_title" placeholder="Title of the lane" > <button ng-click="testScope()">Create</button> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM