我试图在就绪函数中使用$scope.lastName="ThisIsLastname“设置名为”ThisIsLastname“的ng模型的值。我可以设置值,也可以设置它,因为我可以在控制台中看到,但问题是textbox本身没有出现值(textbox没有预先填充)。只有当我单击文本框并在屏幕上的任何位置单击文本框时,文本框才会被预先填充。
<input type="text" ng-model="lastName" /> <!-- this textbox should be prefilled when screen load using ng-model only -->
angular.element(document).ready(function () {
    console.log("is ready now");
    $scope.lastName="ThisIsLastname"; //set value
    console.log($scope.lastName); // I can see that value has been set successfully.
});因此,当我重新加载页面时,我可以看到,一旦就绪函数被调用,这个值就会打印在控制台上,但是我可以看到空的文本框(姓氏的值没有显示在文本框中)。
但是,当我首先单击该空文本框,然后单击屏幕上的任何位置时,文本框的值就会出现。我不知道是什么使它现在出现,但没有出现时,准备函数调用。
我不需要替代的解决方案,我想了解为什么会发生这种情况!
发布于 2020-05-27 18:03:08
在angularjs中,angular.element是jqLite对象。
用归零的$timeout包装代码到triger摘要循环:
console.log("is ready now");
$timeout(function(){      
  $scope.lastName="ThisIsLastname";    
}); 
console.log($scope.lastName); https://stackoverflow.com/questions/62049390
复制相似问题