【问题标题】:ng-style ternary condition always resulting falseng 风格的三元条件总是导致 false
【发布时间】:2017-04-30 05:49:36
【问题描述】:

我正在使用 angularJS 和 node.js 这里出现了一个问题,即“ng-style”三元条件总是结果为假,而它必须为真。 这是我的代码:

           <div class="msg-single" 
               ng-repeat="message in messages"
               ng-style="{'cursor': msessage.toid === socketId ? 'not-allowed':'pointer'}">
               {{message.fromid === socketId ? 'You': message.name }} : {{ message.msg }}
           </div>

在上面的代码中,ng 样式在发送方套接字和接收方套接字(指针)上都导致错误。 而在下一行,message.fromid === socketId 它工作正常意味着它在相同的 socketId 处结果为 true,但在发送者正在发送消息的另一个套接字 id 处结果为 false。

相关的.js代码为:

socket.on('getMsg', (data) => {

            socket.broadcast.to(data.toid).emit('sendMsg',{
                msg:data.msg,
                name:data.name,
                fromid:data.fromid,
                toid:data.toid
            });
            socket.emit('sendMsg',{
                msg:data.msg,
                name:data.name,
                fromid:data.fromid,
                toid:data.toid
            });

        });

socket.on('sendMsg', (data) => {
    //console.log('send');
            $scope.messages.push(data);
       });

你能告诉我可能的原因是什么吗?和解决方案。

【问题讨论】:

  • msessage.toid 拼写错误

标签: angularjs node.js socket.io


【解决方案1】:

我认为这应该适合你:

ng-style="message.toid === socketId ? {'cursor':'not-allowed'} : {'cursor':'pointer'}"

或者:

ng-style="{{message.toid === socketId}} ? {'cursor':'not-allowed'} : {'cursor':'pointer'}"

【讨论】:

    【解决方案2】:

    我不知道为什么,但我认为你可以试试这个:

    ng-style="{message.toid === socketId} && {'cursor': 'not-allowed'} || {'cursor':'pointer'}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 2019-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-24
      • 2019-08-24
      相关资源
      最近更新 更多