gpt4 book ai didi

javascript - AngularJS + dotdotdot 插件不起作用

转载 作者:行者123 更新时间:2023-11-28 07:06:52 26 4
gpt4 key购买 nike

我在 AngularJS 中使用 dotdotdot JQuery 插件时遇到了一个小问题。这是我的 HTML 代码:

<div class="col-md-2 col-sm-6 col-xs-12" ng-repeat="video in videos track by $index">
<a href="{{thumbsPath + video.thumbnail}}" class="thumb">
<div class="thumbnail">
<img ng-src="{{thumbsPath + video.thumbnail}}" alt="">
<div class="caption">
<p class="video-title" dotdotdot>{{video.name}}</p>
</div>
</div>
</a>
</div>

这是我的 dotdotdot 指令:

videoControllers.directive('dotdotdot', function() {
return function(scope, element, attrs) {
$(element).dotdotdot({'watch':true});
};
});

我想让我的 {{video.name}} 更短,但是当我添加 dotdotdot 时 - 它不会显示 {{video.name}} 的内容,而是显示文本:“{{video.name} }”。我几乎阅读了有关此主题的所有帖子,但没有任何帮助。

最佳答案

您应该在指令中调用scope.$apply。这将导致您的 Controller 更新模型。请参阅下面的示例

编辑

需要添加 require: 'ngModel' 这样每次都会更新

编辑2

事实证明,在 ng-repeat 中使用该指令时,您应该使用该指令的编译属性,而不是链接

var app = angular.module('myApp', []);

app.controller('myController', function($scope) {
$scope.videos = [{
name: 'Pool',
thumbnail: 'http://img.youtube.com/vi/dXo0LextZTU/0.jpg'
}, {
name: 'Pool2',
thumbnail: 'http://www.shender.com/db_picture/pro19/201105251714246730.jpg'
}];
});

app.directive('dotdotdot', function() {
return {
restrict: 'A',
compile: function(scope, element, attrs) {
return {
pre: function(scope, element, attrs) {
$(element).dotdotdot({
'watch': true
});
}
};
}
};
});
<!DOCTYPE html>
<html ng-app="myApp">

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jQuery.dotdotdot/1.7.4/jquery.dotdotdot.min.js"></script>
<link rel="stylesheet" href="style.css" />
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
</head>

<body ng-controller="myController">
<div class="col-md-2 col-sm-6 col-xs-12" ng-repeat="video in videos track by $index">
<a href="{{thumbsPath + video.thumbnail}}" class="thumb">
<div class="thumbnail">
<img ng-src="{{thumbsPath + video.thumbnail}}" alt="">
<div class="caption">
<p class="video-title" dotdotdot>{{video.name}}</p>
</div>
</div>
</a>
</div>
</body>

</html>

关于javascript - AngularJS + dotdotdot 插件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31637189/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com