AngularJS on输入指令表单输入无保存模型值

时间:2022-06-21 11:13:36

I have the following on a page - full code in this Plunker

我在页面上有以下内容 - 此Plunker中的完整代码

There is a custom onEnter directive that calls a function when enter is pressed on a chat form input. Code snippet below

有一个自定义onEnter指令,当在聊天表单输入上按下enter时调用一个函数。下面的代码片段

//**HTML View**
<div ng-controller="mainCtrl">
    <ul>
      <li ng-repeat="chat in chatMessages">
        {{chat.username}}<br/>
           {{chat.message}}
        </li>
    </ul>
</div>
<form id="chatForm" name="chatForm" ng-controller="formCtrl">
    <label for="chat-username">User: </label>
    <input type="text" id="chat-username" class="chat__username" ng-model="chat.username" required>
    <label for="chat-input">Chat: </label> 
    <input type="text" id="chat-input" class="chat__input" on-enter="sendChat()" ng-model="chat.message" required>
    <a href="#" class="chat__submit icon-comments" id="chat-submit"  ng-click="sendChat()" ng-disabled="isChatValid()">Chatme</a>
</form>


//**Javascript**
app.controller('formCtrl',function($scope,Chats){
  $scope.sendChat = function() {
    if($scope.isChatValid()) {
      return;
    }
    console.log(JSON.stringify($scope.chat));
    var msg = {};
    angular.copy($scope.chat,msg);
    Chats.data.push(msg);       
  };

  $scope.isChatValid = function() {
    return $scope.chatForm.$invalid;
  };
});

Problem is the value of the input (message) is not saved into the scope model (chat). If I remove the onenter directive it works. What am I missing here? Any help will be great

问题是输入(消息)的值未保存到范围模型(聊天)中。如果我删除onenter指令就可以了。我在这里想念的是什么?任何帮助都会很棒

4 个解决方案

#1


12  

Ok figured it out. Turns out when you define a scope object in your directive you create a new child scope. Details found here:

好吧想通了。在指令中定义范围对象时,您会创建一个新的子范围。详情见:

Directive with scope breaking ngmodel of an input

范围破坏输入的ngmodel的指令

To fix this I removed the scope declaration and used the value of the function from attrs object.

为了解决这个问题,我删除了范围声明并使用了attrs对象中的函数值。

Directive looks like this now:

指令现在看起来像这样:

app.directive('onEnter',function() {

  var linkFn = function(scope,element,attrs) {
    element.bind("keypress", function(event) {
      if(event.which === 13) {
        scope.$apply(function() {
      scope.$eval(attrs.onEnter);
        });
        event.preventDefault();
      }
    });
  };

  return {
    link:linkFn
  };
});

Full code in plunker

plunker中的完整代码

#2


3  

For angular 1.2 i wrote like:

对于角度1.2我写道:

var app = angular.module('application', []);

app.directive('onEnter', function() {
    return {
        scope: {onEnter: '&'},
        link: function(scope, element) {
            console.log(scope);
            element.bind("keydown keypress", function(event) {
                if(event.which === 13) {
                    scope.onEnter();
                    scope.$apply();
                }
            });
        }
    }
});

#3


1  

You should use $parent to refer to the chat object like this

您应该使用$ parent来引用这样的聊天对象

$parent.chat.message

Because you use the directive which declares a child scope. Here is the full html:

因为您使用声明子范围的指令。这是完整的HTML:

<input type="text" id="chat-input" class="chat__input" on-enter="sendChat()" ng-model="$parent.chat.message" required>

#4


0  

In reply to @zool:

回复@zool:

I had to modify your answer as follows to avoid my onEnter function being called twice:

我必须按如下方式修改你的答案,以避免我的onEnter函数被调用两次:

[...]
if(event.which === 13) {
    event.preventDefault();
    scope.onEnter();
    scope.$apply();
}
[...]

Also see AngularJS form not submitting when pressing enter

另请参阅按Enter键时AngularJS表单未提交

#1


12  

Ok figured it out. Turns out when you define a scope object in your directive you create a new child scope. Details found here:

好吧想通了。在指令中定义范围对象时,您会创建一个新的子范围。详情见:

Directive with scope breaking ngmodel of an input

范围破坏输入的ngmodel的指令

To fix this I removed the scope declaration and used the value of the function from attrs object.

为了解决这个问题,我删除了范围声明并使用了attrs对象中的函数值。

Directive looks like this now:

指令现在看起来像这样:

app.directive('onEnter',function() {

  var linkFn = function(scope,element,attrs) {
    element.bind("keypress", function(event) {
      if(event.which === 13) {
        scope.$apply(function() {
      scope.$eval(attrs.onEnter);
        });
        event.preventDefault();
      }
    });
  };

  return {
    link:linkFn
  };
});

Full code in plunker

plunker中的完整代码

#2


3  

For angular 1.2 i wrote like:

对于角度1.2我写道:

var app = angular.module('application', []);

app.directive('onEnter', function() {
    return {
        scope: {onEnter: '&'},
        link: function(scope, element) {
            console.log(scope);
            element.bind("keydown keypress", function(event) {
                if(event.which === 13) {
                    scope.onEnter();
                    scope.$apply();
                }
            });
        }
    }
});

#3


1  

You should use $parent to refer to the chat object like this

您应该使用$ parent来引用这样的聊天对象

$parent.chat.message

Because you use the directive which declares a child scope. Here is the full html:

因为您使用声明子范围的指令。这是完整的HTML:

<input type="text" id="chat-input" class="chat__input" on-enter="sendChat()" ng-model="$parent.chat.message" required>

#4


0  

In reply to @zool:

回复@zool:

I had to modify your answer as follows to avoid my onEnter function being called twice:

我必须按如下方式修改你的答案,以避免我的onEnter函数被调用两次:

[...]
if(event.which === 13) {
    event.preventDefault();
    scope.onEnter();
    scope.$apply();
}
[...]

Also see AngularJS form not submitting when pressing enter

另请参阅按Enter键时AngularJS表单未提交