在回调方法中访问controller $ scope

时间:2021-01-17 15:53:24

Heyho!

I am using ROS (Robot Operating System) in combination with roslib.js and angular.js.

我正在使用ROS(机器人操作系统)与roslib.js和angular.js结合使用。

A method is calling a service, this is done via network and takes some time. The best way would be, that this call is blocking. But it isn’t. The wanted data is received from the callback, but it is not possible to copy the data somewhere else to the controller.

一种方法是调用服务,这是通过网络完成的,需要一些时间。最好的方法是,这个调用是阻塞的。但事实并非如此。从回调接收所需数据,但无法将数据复制到控制器的其他位置。

In my opinion it is a problem of Scopes. But I already implemented a Factory for receiving the data and tried a dirty hack with $rootScope but nothing worked...

在我看来,这是范围的问题。但是我已经实现了一个用于接收数据的工厂并尝试使用$ rootScope进行了一次肮脏的黑客攻击但没有任何工作......

Here is some descriptive code:

这是一些描述性代码:

View:

Here a ng-repeat listens to the variable list in the controller rosCtrl

这里ng-repeat监听控制器rosCtrl中的变量列表

JS:

Controller rosController
  var rosCtrl = this;
  rosCtrl.list = [];

  this.receiveData = function(){
    callService(input, function(result){
      ..
      .. some logging for debugging ..
      rosCtrl.list.push({name: "a", type: 1});
    }
  }

The data is successfully received and with other methods I can add items to the list and the view is updated. So there isn’t any problem in the mechanism.

数据已成功接收,并且使用其他方法,我可以将项目添加到列表中并更新视图。所以这个机制没有任何问题。

Hope I could give you an understanding of the problem. If not, please let me know, so I will try to explain it more detailed.

希望我能帮你理解这个问题。如果没有,请告诉我,所以我会尝试更详细地解释它。

Thank you in advance!

先感谢您!

2 个解决方案

#1


0  

How about something like this:

这样的事情怎么样:

http://plnkr.co/edit/jPH4d5RiinxOGuxMjSRr?p=preview

(function(angular) {
  'use strict';
angular.module('scopeExample', [])
  .controller('rosController', ['$scope', function($scope) {
    $scope.list = ['World'];

    $scope.sayHello = function() {

      setTimeout(function callService(){
        $scope.list.push('Another '+Math.random().toString().split('.')[1]);
      },100);
    }
  }]);
})(window.angular);

#2


0  

Thanks for both of your answers. I tried your ideas, but they didn’t work either. Meanwhile I found the problem: Another programmer added two times the same controller nested in another controller and that's why the content of the list was empty all the time. Sometimes life is that easy..... Gnarf....

谢谢你的两个答案。我试过你的想法,但他们也没有用。同时我发现了问题:另一个程序员在同一控制器中添加了两次嵌套在另一个控制器中,这就是为什么列表的内容一直是​​空的。有时生活就那么容易...... Gnarf ....

#1


0  

How about something like this:

这样的事情怎么样:

http://plnkr.co/edit/jPH4d5RiinxOGuxMjSRr?p=preview

(function(angular) {
  'use strict';
angular.module('scopeExample', [])
  .controller('rosController', ['$scope', function($scope) {
    $scope.list = ['World'];

    $scope.sayHello = function() {

      setTimeout(function callService(){
        $scope.list.push('Another '+Math.random().toString().split('.')[1]);
      },100);
    }
  }]);
})(window.angular);

#2


0  

Thanks for both of your answers. I tried your ideas, but they didn’t work either. Meanwhile I found the problem: Another programmer added two times the same controller nested in another controller and that's why the content of the list was empty all the time. Sometimes life is that easy..... Gnarf....

谢谢你的两个答案。我试过你的想法,但他们也没有用。同时我发现了问题:另一个程序员在同一控制器中添加了两次嵌套在另一个控制器中,这就是为什么列表的内容一直是​​空的。有时生活就那么容易...... Gnarf ....