已解决:Update Angular model after setting input value with jQuery
这是带有<input>
元素的代码。它与AngularJS一起运行。
我需要为它模仿按键。
如果我使用模仿按键事件的$()。val()或autotype库,AngularJS脚本不会处理它。我不能按“创建组”按钮。
但如果我手动输入,按钮可以正常工作。
<div class="md_simple_modal_body">
<form class="modal_simple_form ng-pristine ng-valid" ng-submit="updateGroup()">
<h4 my-i18n="group_create_modal_title">Create group</h4>
<div class="md-input-group md-input-animated" my-labeled-input="">
<label class="md-input-label" my-i18n="group_create_name">Group name</label>
<input class="md-input ng-pristine ng-valid ng-empty ng-touched" type="text" ng-model="group.name" my-focused="">
</div>
</form>
</div>
<div class="md_simple_modal_footer">
<button class="btn btn-md" ng-click="$dismiss()" my-i18n="modal_cancel">Cancel</button>
<button class="btn btn-md btn-md-primary" ng-class="{disabled: group.creating}" ng-click="createGroup()" ng-bind="group.creating ? 'group_create_submit_active' : 'group_create_submit' | i18n" ng-disabled="group.creating">Create group</button>
</div>
</div>
控制器中的功能:
$scope.createGroup = function () {
if (!$scope.group.name) {
return
}
$scope.group.creating = true
var inputUsers = []
angular.forEach($scope.userIDs, function (userID) {
inputUsers.push(AppUsersManager.getUserInput(userID))
})
return MtpApiManager.invokeApi('messages.createChat', {
title: $scope.group.name,
users: inputUsers
}).then(function (updates) {
ApiUpdatesManager.processUpdateMessage(updates)
if (updates.updates && updates.updates.length) {
for (var i = 0, len = updates.updates.length, update; i < len; i++) {
update = updates.updates[i]
if (update._ == 'updateNewMessage') {
$rootScope.$broadcast('history_focus', {peerString: AppChatsManager.getChatString(update.message.to_id.chat_id)
})
break
}
}
$modalInstance.close()
}
})['finally'](function () {
delete $scope.group.creating
})
}
我试过了:
$("form[ng-submit='updateGroup()']").find("input[ng-model='group.name']").val("12223");
和
$("form[ng-submit='updateGroup()']").find("input[ng-model='group.name']").autotype("12223"); // With library. Link poster earlier.
答案 0 :(得分:0)
在另一个问题中找到答案:
Update Angular model after setting input value with jQuery
在使用jQuery更改输入值后,只需要使用$(element).trigger('input');
。