jquery - AngularJS ng-repeat 与 jquery 冲突
问题描述
我在下面有一个 angularjs 代码,它可以很好地显示数据库中的记录。因为内容很长,我想通过jquery添加阅读更多按钮。如果我在angularjs ng-repeat函数之外调用 jquery 数据,则ReadMore 按钮有效。例如
<div class="text">
Hi! Introducing myself as wasco man. I am an arena where people can visit by typing wasco from any where in the world. The purpose of my birth is to give solutions in all ramifications. Coming to my work is a web designer and a developer who has got vast experience in giving solutions
on JQuery, Ajax and PHP from small websites to big portals and high performing enterprise web applications. The innet passion to teach people has provoked him to introduce me to this world to fulfill his desire.
<div class="post" ng-repeat='post in posts'>
</div>
但现在的问题是,当我尝试在angularjs ng-repeat函数中调用 Jquery Data 时,readmore 按钮不起作用。似乎 angularjs 与 jquery 冲突,如下面的代码所示
<div class="post" ng-repeat='post in posts'>
<div class="text">
Hi! Introducing myself as wasco man. I am an arena where people can visit by typing wasco from any where in the world. The purpose of my birth is to give solutions in all ramifications. Coming to my work is a web designer and a developer who has got vast experience in giving solutions
on JQuery, Ajax and PHP from small websites to big portals and high performing enterprise web applications. The innet passion to teach people has provoked him to introduce me to this world to fulfill his desire.
</div> </div>
下面是整个代码..
<!doctype html>
<html>
<head>
<link href="style.css" type="text/css" rel="stylesheet" />
<!--read more text starts here-->
<script src="jquery.min.js"></script>
<script>
$(document).ready(function(){
function breakWords(paragraph, showtext){
var words = paragraph.split(' ');
var newText = '';
for(i=0; i<words.length; i++){
if(i<= showtext){
newText += words[i] + ' ';
}else {
if (i == (showtext + 1)) newText += '... <span class="fulltext hide">';
newText += words[i] + ' ';
if (words[i+1] == null) newText += '</span><a href="#" class="links"> » read more</a>';
}
}
return newText;
}
$('.text').each(function () {
$(this).html(breakWords($(this).html(), 50));
$(this).children('span').hide();
}).click(function () {
var fulltext = $(this).children('span.fulltext');
var links = $(this).children('a.links');
if (fulltext.hasClass('hide')) {
fulltext.show(200);
links.html(' » hide');
fulltext.removeClass('hide');
} else {
fulltext.fadeOut(100);
links.html(' « read more');
fulltext.addClass('hide');
}
return false;
});
});
</script>
<!--read more text ends-->
</head>
<body ng-app='myapp'>
<div class="content" ng-controller='fetchCtrl' >
<div class="post" ng-repeat='post in posts'>
<div class="text">
Hi! Introducing myself as wasco man. I am an arena where people can visit by typing wasco from
any where in the world.
The purpose of my birth is to give solutions in all ramifications.
Coming to my work is a web designer and a developer who has got vast experience in giving solutions
on JQuery, Ajax and PHP from small websites to big portals and high performing enterprise web
applications. The innet passion to teach people has provoked him to introduce me to this world to fulfill his desire.
Thanks a lot !!!</div>
<h1 >{{ post.title }}</h1>
<div class="post-text">
{{ post.content }}
</div>
</div>
</div>
<!-- Script -->
<script src="angular.min.js"></script>
<script>
var fetch = angular.module('myapp', []);
fetch.controller('fetchCtrl', ['$scope', '$http', function ($scope, $http) {
// Fetch post data
$scope.getPosts = function(){
$http({
method: 'post',
url: 'likeunlike.php',
data: {request: 1}
}).then(function successCallback(response) {
$scope.posts = response.data;
});
}
$scope.getPosts(); // Fetch post data
}
]);
</script>
</body>
</html>
解决方案
推荐阅读
- angular - Jest 迁移时令牌无效或意外
- wordpress - 如何从订单中删除结帐页面上的缺货产品并显示它们
- android - 如何从 Android 中的微调器中删除 4 和 5 索引
- apache-spark - 在 databricks 运行时 6.4 中找不到 Spark 2.4.5 from_csv 函数
- python - AttributeError:“NoneType”对象没有属性“GetLayer”
- python - 如何加快此 UTC 转换过程?
- java - Java 将 dateTime 转换为给定的格式
- reporting-services - SSRS 通过单击 ID 字段将参数传递到同一页面中的子报表
- ios - 更改 ContextMenu iOS 外观(背景和突出显示颜色)
- javascript - Odoo11 EE 如何去除签名广告