jQuery如何在自定义指令中响应$scope的变化?
angular - How can jQuery inside a custom directive respond to a $scope change?
jQuery如何在自定义指令中响应$scope的变化?
<div id="wrapper" scrolldirective>
<div id="scroller">Hello this is a test<div>
</div>
注意:$timeout只在指令内部,用于将。scrolltop()推到更新周期的末尾,就像这个线程一样。jQuery:水平滚动控制器或指令中的div
myapp.directive('scrolldirective', function($timeout) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
$timeout(function() {
$(element).scrollTop( scope.mytop );
}, 1);
}
};
});
最后是一些$scope代码,我想让指令对
做出反应$scope.mytop = 500;
$timeout(function(){
$scope.setPosition(50);
},1000);
$scope.setPosition = function(arg){
$log.log('===> ',arg);
$scope.mytop = arg;
$scope.$apply(); // ???
}
编辑:这里是一个jsFiddle,其中包含了运行该程序所需的所有最小代码。不幸的是,jsFiddle的UI将自己的滚动条添加到面板中,这会干扰到这一点!嗯……。但至少所有的代码都在那里,如果你想复制的话。http://jsfiddle.net/PrimeLens/CV5L8/
我没有收到Ian的回复(见评论),但他的话让我找到了解决方案。我在这里发布了一组工作代码https://github.com/PrimeLens/angular-MVP-custom-directive-jquery-with-data-binding
scope.$watch('variableName', function() {
//
});
相关文章:
- AngularJS:ng之后,重复$scope值未按预期更新
- 单击页面上的链接后高度发生变化
- $ionicplatform内的$scope不;不起作用
- 使用$scope方法时的ControllerAs语法
- 如何将ngrepeat下的ngmodel绑定到$scope
- React redux初始化功能,无论状态变化如何
- 另一个if(!$scope.$$phase)$scope$apply()szenario-需要帮助才能通关
- 角度ng变化或ng点击选择can'不起作用
- 每个选择器的Jquery css颜色都在变化,但字体大小却没有变化
- 使用AngularJS Directive WHITOUT$scope创建一个动态html元素
- $scope变量被视为字符串AngularJs
- 当$scope模型发生变化时,角度视图不会发生变化
- ng-在 Angularjs 中重复 当$scope变化时
- 棱角分明,观察$scope的变化
- 当角度$scope发生变化时,不要更新 HTML
- 失败绑定 Angular 承诺$scope变化
- AngularJs:如何在值变化时启动动画(ng-scope)
- 如何准确地找出$scope中发生了什么变化$看
- jQuery如何在自定义指令中响应$scope的变化?
- 仅当$scope数据发生变化时更新DOM