无法读取“未定义”的属性推送

克里斯里(Krisalay)
app.controller('ctrl', function($scope){
    var socket = io.connect();

    this.messages = [];

    this.sendMessage = function(){
        socket.emit('new message', this.input_message);
        this.input_message = '';
    }

    socket.on('new message', function(data){
        this.messages.push(data);
    });
});

我正在获取套接字发出的数据,但是当我尝试将该数据推入messages数组时,它显示了错误cannot read the property push of undefined我在做什么错?

鲈鱼

这是行不通的,因为您要this在套接字范围(this.messages.push(data))中进行调用,该范围与控制器范围不同。这就是为什么this.messagesundefined,因为它是在控制器的功能范围内声明的,但不是在插座回调函数范围。在javascript中,每个函数都有其自己的作用域(箭头函数除外)。

建议

app.controller('ctrl', function($scope){
   var ctrlContext = this;
   var socket = io.connect();

   //using the this keyword here means that
   //it's in the controller function scope.
   this.messages = [];

   this.sendMessage = function(){
      socket.emit('new message', this.input_message);
      this.input_message = '';
   }

   socket.on('new message', function(data){
      //inside the callback function we have a different
      //scope, which means that this.messages will be undefined.
      //but by referencing the controller function scope, you
      //are now able to use the messages property set the
      //outer (controller function) scope.
      ctrlContext.messages.push(data);

      //as you stated, you will also have to call $scope.$apply
      //or else the digest cycle will not be triggered.
   });
});

但是,如果您想this在套接字回调函数中使用关键字,则可以通过使用function.prototype.bind方法来实现,该方法允许您设置执行函数的上下文。

例子

var person = {
   name: 'John Doe';
}

//Create function and bind to context person    
var changeName = function() {
   this.name = 'Peter Smith';
}.bind(person);

console.log(person); // {name: 'John Doe'}

changeName(person);

console.log(person); // {name: 'Peter Smith'}

对于您的解决方案,它将类似于:

app.controller('ctrl', function($scope){
   var socket = io.connect();

   this.messages = [];

   var callback = function(data){
      this.messages.push(data);
   }.bind(this);

   socket.on('new message', callback);
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

无法读取未定义的属性“推送”

来自分类Dev

无法读取未定义的属性“推送”?

来自分类Dev

无法读取未定义的属性推送

来自分类Dev

reactjs-无法读取未定义的属性推送

来自分类Dev

离子设置推送-无法读取未定义的属性

来自分类Dev

Polymerfire - 无法读取未定义的属性“推送”

来自分类Dev

无法读取数组上未定义的属性“推送”

来自分类Dev

无法读取未定义错误的属性推送

来自分类Dev

Angular - TypeError:无法读取未定义的属性“推送”

来自分类Dev

无法读取未定义的 Angular/Typescript 的属性“推送”

来自分类Dev

Javascript Uncaught TypeError:无法读取未定义的属性“推送”

来自分类Dev

无法读取未定义的 Typescript / Angular 6 的属性“推送”

来自分类Dev

JavaScript 错误:无法读取未定义的属性“推送”

来自分类Dev

React Native Navigation 如何推送屏幕?(无法读取未定义的属性推送)

来自分类Dev

无法读取未定义/未定义的属性

来自分类Dev

TypeError:无法读取未定义的属性“未定义”

来自分类Dev

无法读取未定义的属性“ $ valid”

来自分类Dev

无法读取未定义的属性“ ObjectID”

来自分类Dev

angularjs无法读取未定义的属性

来自分类Dev

无法读取未定义的属性“ main”

来自分类Dev

无法读取未定义的属性“ forEach”

来自分类Dev

无法读取未定义的属性“ MyProperty”

来自分类Dev

无法读取未定义的属性“ split”

来自分类Dev

无法读取未定义的属性“ $ scope”

来自分类Dev

无法读取未定义的属性“协议”

来自分类Dev

无法读取未定义的属性“ toJSON”

来自分类Dev

无法读取未定义的属性“ attr”

来自分类Dev

无法读取未定义的属性“ helpers”

来自分类Dev

无法读取未定义的属性“ setBounds”

Related 相关文章

  1. 1

    无法读取未定义的属性“推送”

  2. 2

    无法读取未定义的属性“推送”?

  3. 3

    无法读取未定义的属性推送

  4. 4

    reactjs-无法读取未定义的属性推送

  5. 5

    离子设置推送-无法读取未定义的属性

  6. 6

    Polymerfire - 无法读取未定义的属性“推送”

  7. 7

    无法读取数组上未定义的属性“推送”

  8. 8

    无法读取未定义错误的属性推送

  9. 9

    Angular - TypeError:无法读取未定义的属性“推送”

  10. 10

    无法读取未定义的 Angular/Typescript 的属性“推送”

  11. 11

    Javascript Uncaught TypeError:无法读取未定义的属性“推送”

  12. 12

    无法读取未定义的 Typescript / Angular 6 的属性“推送”

  13. 13

    JavaScript 错误:无法读取未定义的属性“推送”

  14. 14

    React Native Navigation 如何推送屏幕?(无法读取未定义的属性推送)

  15. 15

    无法读取未定义/未定义的属性

  16. 16

    TypeError:无法读取未定义的属性“未定义”

  17. 17

    无法读取未定义的属性“ $ valid”

  18. 18

    无法读取未定义的属性“ ObjectID”

  19. 19

    angularjs无法读取未定义的属性

  20. 20

    无法读取未定义的属性“ main”

  21. 21

    无法读取未定义的属性“ forEach”

  22. 22

    无法读取未定义的属性“ MyProperty”

  23. 23

    无法读取未定义的属性“ split”

  24. 24

    无法读取未定义的属性“ $ scope”

  25. 25

    无法读取未定义的属性“协议”

  26. 26

    无法读取未定义的属性“ toJSON”

  27. 27

    无法读取未定义的属性“ attr”

  28. 28

    无法读取未定义的属性“ helpers”

  29. 29

    无法读取未定义的属性“ setBounds”

热门标签

归档