好的..我试过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>
绑定#addItem的点击函数的函数应该在指令中。该指令具有与范围交互的各种方式。请参阅http://docs.angularjs.org/guide/directive –
您能否澄清在您的案例中“超出范围”的含义?你能分享一些标记,以便我们可以看到你正在尝试添加'click'处理程序的元素吗?虽然可以从AngularJS世界的外部访问$ scope对象(http://stackoverflow.com/a/10508731/1418796),但通常不需要... –