Javascript AngularJS:Longpress指令不使用嵌套指令上控制器视图中的函数
在我不得不调整指令结构之前,onLongPress指令已经生效 现在它不起作用了,我也不知道为什么,也许有人能帮我找到我的错误 onLongPress应该从分配给视图的控制器调用一个方法,它不会,但是如果我添加$scope.someFn=函数;在指令内部,它可以工作,但我需要从视图的控制器调用一个方法 我怎样才能解决这个问题 下一个例子是:Javascript AngularJS:Longpress指令不使用嵌套指令上控制器视图中的函数,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,在我不得不调整指令结构之前,onLongPress指令已经生效 现在它不起作用了,我也不知道为什么,也许有人能帮我找到我的错误 onLongPress应该从分配给视图的控制器调用一个方法,它不会,但是如果我添加$scope.someFn=函数;在指令内部,它可以工作,但我需要从视图的控制器调用一个方法 我怎样才能解决这个问题 下一个例子是: <my-tabs> <tab-one></tab-one> <tab-two></ta
<my-tabs>
<tab-one></tab-one>
<tab-two></tab-two>
...
</my-tabs>
我的标签
昂隆出版社
更新:
如果我添加$scope.doOtherthing=function{};在myTabs指令中,它可以工作,但正如我所说,我需要从视图的控制器中获取该函数:/
谢谢。您能否尝试将tabOne指令中的scope:false改为true,看看这是否能解决问题?改为true后,它就不起作用了。在onLongPress指令中添加Fn可以使其工作,但这不是我想要实现的。有没有可能您可以设置JSFIDLE?好的,我会尝试,给我一些时间,我会设置它。无法使FIDLE工作,甚至无法显示文本。JSFIDLE获胜:/
<ul>
<li ng-click="doSomething()" on-long-press="doOtherthing()">
Some nice item.
</li>
</ul>
app.directive('tabOne', function() {
return {
restrict: 'E',
scope: false,
transclude: false,
templateUrl: 'templates/tab-one.html'
};
});
app.directive('myTabs', function() {
return {
restrict: 'E',
templateUrl: 'templates/my-tabs.html',
scope: {
myMenues: '=',
selectedTab: '=',
general: '=',
userPoints: '=',
changetab: '&ngClick'
},
controller: function($scope) {
// Some code to add functionality. Nothing fancy.
}
};
});
app.directive('onLongPress', function($timeout) {
return {
restrict: 'A',
link: function($scope, $elm, $attrs) {
$elm.bind('touchstart', function(evt) {
// Locally scoped variable that will keep track of the long press
$scope.longPress = true;
// We'll set a timeout for 600 ms for a long press
$timeout(function() {
if ($scope.longPress) {
// If the touchend event hasn't fired,
// apply the function given in on the element's on-long-press attribute
$scope.$apply(function() {
$scope.$eval($attrs.onLongPress)
});
}
}, 600);
});
$elm.bind('touchend', function(evt) {
// Prevent the onLongPress event from firing
$scope.longPress = false;
// If there is an on-touch-end function attached to this element, apply it
if ($attrs.onTouchEnd) {
$scope.$apply(function() {
$scope.$eval($attrs.onTouchEnd)
});
}
});
}
};
});