Javascript 数据绑定没有';在特定情况下不工作(AngularJS)
这是我的问题:Javascript 数据绑定没有';在特定情况下不工作(AngularJS),javascript,html,angularjs,dom,angularjs-scope,Javascript,Html,Angularjs,Dom,Angularjs Scope,这是我的问题: 修改模型“urlservicerest”后单击按钮时,我的属性“$scope.urlservicerest”保持初始化为“hello” 我尝试使用$scope.$apply更新数据,但没有成功。我不明白为什么“$scope.urlservicerest”没有更新,而数据绑定已经创建 //file.html <input type="input" ng-model="urlservicerest"></ <button type="button" ng-
修改模型“urlservicerest”后单击按钮时,我的属性“$scope.urlservicerest”保持初始化为“hello”
我尝试使用$scope.$apply更新数据,但没有成功。我不明白为什么“$scope.urlservicerest”没有更新,而数据绑定已经创建
//file.html
<input type="input" ng-model="urlservicerest"></
<button type="button" ng-click="refresh()"> Rafraichir </button>
//script.js:
var field = $scope.urlservicerest = "hello";
$scope.refresh = function () {
alert(field);
}
//file.html
我更新了一个关于JS小提琴的演示,希望能对你有所帮助
我认为您需要将
添加到页面中角度绑定到作用域属性。因此,在您的情况下,$scope.urlservicerest
将在输入值更改时双向绑定
您正在提醒字段
的值,该值不是双向绑定的
为scoped属性添加第二个警报表明$scope.urlservicerest
已正确绑定:
$scope.refresh = function () {
alert(field);
alert($scope.urlservicerest);
}
演示这一点。您是否更新了
标记,如
?不要期望字段
保留其值(绑定到范围)。这就是$scope
的作用。