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

时间:2017-05-03 04:16:20

标签: javascript angularjs angular-ngmodel

我在另一个地方使用了这种技术并且它有效但在这种情况下它无法正常工作。这里lane_title显示未定义。我在这里错过了什么。

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

在控制器中:

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

1 个答案:

答案 0 :(得分:0)

似乎工作正常。但请确保初始化模型变量。否则,它将打印为undefined

&#13;
&#13;
angular.module("app",[])
.controller("ctrl",function($scope){

$scope.lane_title = "";
$scope.testScope = function(){
    alert($scope.lane_title);
}
})
&#13;
<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>
&#13;
&#13;
&#13;