2012-11-07 126 views
2

好的..我试过angular.js。太棒了。我印象深刻。我可以得到绑定和东西。很酷。 现在如果我需要从$ scope之外访问我的数据呢?比方说,我有一个signalR集线器发送一些数据和函数来拦截它,并且应该添加一个新项目或修改现有的项目。我怎么做?你能告诉我这个例子,我怎样才能从click手柄访问$scope.twitterResult

<script> 
    angular.module('Twitter', ['ngResource']) 

    function TwitterCtrl($scope, $resource){ 
     $scope.twitter = $resource('http://search.twitter.com/:action', 
     {action: 'search.json', q: 'obama', callback:'JSON_CALLBACK'}, 
     {get:{method:'JSONP'}}); 
     $scope.doSearch = function(){ 
     $scope.twitterResult = $scope.twitter.get(); 
     } 
    } 

    $(function(){ 
     $('#addItem').click(function(){ 
      // add a row to $scope.twitterResult 
     }); 
    }); 
</script> 

<body> 
    <div data-loading></div> 
    <div ng-controller='TwitterCtrl' ng-init="doSearch()"> 
     <ul> 
      <li ng-repeat='tweet in twitterResult.results'><p> {{tweet.text}}</p></li> 
     </ul> 
    </div> 
</body> 
+1

绑定#addItem的点击函数的函数应该在指令中。该指令具有与范围交互的各种方式。请参阅http://docs.angularjs.org/guide/directive –

+0

您能否澄清在您的案例中“超出范围”的含义?你能分享一些标记,以便我们可以看到你正在尝试添加'click'处理程序的元素吗?虽然可以从AngularJS世界的外部访问$ scope对象(http://stackoverflow.com/a/10508731/1418796),但通常不需要... –

回答

2

一个更好的办法是换你“信号枢纽”在AngularJS服务。看看我的博客文章using web sockets with AngularJS,特别是“与Socket.IO交互”。

为什么你写的:

$(function(){ 
    $('#addItem').click(function(){ 
     // add a row to $scope.twitterResult 
    }); 
}); 

而且不只是用ng-click?这是一些第三方代码或小部件?等待这些,我会尽力为您提供建议并撰写一些示例代码。

如果您必须注册事件处理程序,则应该通过directive来完成。否则,当您开始管理这些超出外部角度的事件绑定的生命周期时,事情会变得复杂。

+0

不,它实际上与点击无关......我只是想知道如何从外部访问$ scope ...我要检查您的帖子......谢谢 – Agzam

-1

你也许可以做这样的事情,但我不知道这是否是最好的主意:

var myTwitterScope; 
angular.module('Twitter', ['ngResource']) 

function TwitterCtrl($scope, $resource){ 
    $scope.twitter = $resource('http://search.twitter.com/:action', 
    {action: 'search.json', q: 'obama', callback:'JSON_CALLBACK'}, 
    {get:{method:'JSONP'}}); 
    $scope.doSearch = function(){ 
     myTwitterScope = $scope; 
     $scope.twitterResult = $scope.twitter.get(); 
    } 
} 

$(function(){ 
    $('#addItem').click(function(){ 
     // add a row to $scope.twitterResult 
     myTwitterScope.twitterResult.push(...); // or however you would do this. 
    }); 
}); 

正如其他人所说,这是不干净的解决方案。

+0

然后我需要保留全局变量对于我需要注意的所有集合。我不确定这是否正确。 – Agzam

+0

是的,我同意这不是一个干净的解决方案,但我不熟悉angularjs知道更好的解决方案。 – Shmiddty

+0

,并且这不会自动通知View有关更改。更正:看起来您可以在更改后调用myTwitterScope。$ apply()!适用于我 – Agzam

1

一般的答案是:你不要简单地从外面弄乱范围。

但是你的要求是真实的。

因此,为了做你想做的事情,你需要在范围之外和范围本身之间建立沟通。

最简单的方法是将$ scope导出到窗口并将其混淆,从外部侵入范围。你永远不应该这样做。有龙。

范围应保持其内部状态。

我不完全熟悉angular,但你可以做一些事情来的效果:

function TwitterCtrl($scope, $resource) { 
    // ... 

    $('body').bind('newTweetsArrived', data) { 
     // data contains the new tweets 

     // the decision to accept or not new tweets is made within the control 
     if (in_the_mood_to_accept_new_tweets) { 
      // add new tweets to the $scope.twitterResult 
     } 

     // optionally notify other components that new tweets are accepted 
     // so that they can adjust or whatever 
     $('body').trigger('afterNewTweetsArrived'); 
    } 

} 

// you add new tweets by triggering global custom event 
$(function(){ 
    $('#addItem').click(function(){ 
     $('body').trigger('newTweetsArrived', { ...here_are_the_tweets... }); 
    }); 
}); 
+0

这很聪明。 – Shmiddty

+0

谢谢。我了解到做一些大型Web应用程序的难题。这样耦合就大大减少了。 –