2017-09-05 37 views
0

求解:Update Angular model after setting input value with jQuery如何模仿angularJS輸入的按鍵事件?

這裏的代碼與<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

多一個人同時使用Angular和jQuery(嘆氣)。人們,請同時停用這兩個。像這樣的許多問題直接來自於你試圖讓兩種截然不同的事情一起工作的事實。刪除jQuery,或刪除Angular,你會發現許多問題都會消失。 –

+0

這不是我的代碼,所以我沒有選擇:) – Erami

+0

所以你們加載了兩個非常不同的庫,並試圖混淆它們,導致這種問題並增加了複雜性,顯然沒有辦法否則,並寫一個乾淨的應用程序?這很不幸:/祝你好運,我想 –

回答