ReferenceError:数据未定义
AngularJS传递变量以在控制器中进行过滤
大家好
我还是Angularjs的新手,所以可能完全错了。
我试图显示一个Twitter提要,并使用一个筛选器,该筛选器将使用工厂来解析Twitter提要中的链接。
我收到一个错误ReferenceError:数据未定义
我是否需要将数据从“ TwitterCtrl”传递到“ parseTwitterLinks”
我怎样才能做到这一点?
的HTML看起来像这样
<p><span id="retweet-icon"></span><span ng-bind-html-unsafe="tweet.text | parseTwitterLinks"></span></p>
TwitterCtrl
myApp.controller('TwitterCtrl', ['$scope','$http','twitter','$rootScope','$filter','parseTwitterLinksFilter', function($scope, $http, twitter, $rootScope, $filter, parseTwitterLinksFilter){
$scope.tabId = false;
$scope.tweet = false;
$scope.$on('tabId', function(event, tabId) {
if(tabId != $scope.tabId) {
$scope.tabId = tabId;
if($scope.tabId) {
$scope.fetchTweet();
} else {
$scope.tweet = {};
}
}
});
$scope.fetchTweet = function() {
var dataUrl = '/twitter?tab=' + $scope.tabId;
$http({
method: 'GET',
url: dataUrl
}).success(function(data, status, headers, config){
if(data) {
$scope.tweet = data;
} else {
$scope.tweet = {};
}
}).error(function(data, status, headers, config){
$scope.tweet = {};
});
};
}])
过滤器
myApp.filter('parseTwitterLinks', [function(){
return function(text) {
var tweet = data;
if(typeof tweet.text != 'undefined') {
tweet.text = twitter.parseLinks(tweet.text);
}
$scope.tweet = tweet;
}
return tweet.text;
}]);
工厂解析链接
myApp.factory('twitter', function($rootScope) {
return {
parseLinks: function(str) {
var that = this;
str = str || '';
str = that.parseUrl(str);
str = that.parseUsername(str);
str = that.parseHashtag(str);
return str
},
parseUrl: function(str) {
str = str || '';
return str.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=~]+/g, function(url) {
return '<a href="'+url+'" target="_blank">'+url+'</a>';
});
},
parseUsername: function(str) {
str = str || '';
return str.replace(/[@]+[A-Za-z0-9-_]+/g, function(username) {
return '<a href="http://twitter.com/'+username.replace("@","")+'" target="_blank">'+username+'</a>';
});
},
parseHashtag: function(str) {
str = str || '';
return str.replace(/[#]+[A-Za-z0-9-_]+/g, function(tag) {
return '<a href="http://twitter.com/search?q='+tag.replace("#","%23")+'" target="_blank">'+tag+'</a>';
});
}
}
});
过滤器“ parseTwitterLinks”正在使用“数据”,是否不应该使用“文本”?
myApp.filter('parseTwitterLinks', [function(){
return function(text) {
var tweet = text; // <-- text instead of data ?
if(typeof tweet.text != 'undefined') {
tweet.text = twitter.parseLinks(tweet.text);
}
$scope.tweet = tweet;
}
return tweet.text;
}]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句