我正在编写一个将函数作为参数重新编写的指令,该指令执行并运行函数中包含的任何内容。
用例是:
我的问题是如何推迟执行并将其绑定到promise,以便在方法执行后隐藏微调器。
为了便于说明,我使用了 $ timeout 从3到1的数量,请查看我到目前为止所做的代码:
app.directive('toogleTextLink', function($compile,$q) {
return {
restrict: 'AE',
scope: { callback: "&targetMethod" },
template: '<div><a style="cursor: pointer" ><b>{{text}}</b></a>show value= {{show}} <br/><div ng-class="{previewLoader: show}"></div></div>',
link: function (scope, element, attr) {
scope.value = attr.value;
scope.show = false;
scope.$watch('value', function () {
if (scope.value) {
scope.text = "yes";
} else {
scope.text = "no";
}
});
element.bind('click', function () {
scope.show = true;
scope.value = !scope.value;
scope.$digest();
if (scope.callback) {
var deferred = $q.defer(scope.callback());
deferred.promise.then(function () {
scope.show = false;
console.log("then called");
});
}
});
}
};
});
app.controller('myCtrl', function($scope,$timeout,$q) {
$scope.IsFacebookConnected = false;
$scope.countDown = 3;
$scope.authSocial = function(value, socialNetwork) {
switch (socialNetwork) {
case "facebook":
$scope.IsFacebookConnected = !value;
}
runCounter = function() {
$scope.countDown -= 1;
if ( $scope.countDown > 0)
$timeout(runCounter, 1000);
console.log("timer");
};
runCounter();
};
});
这也是plunker。
答案 0 :(得分:2)
您的代码将类似于:
$scope.tracker = promiseTracker("socialtracker");
$scope.tracker.addPromise(somePromise);
在你看来:
<div ng-show="tracker.active()">Loading...</div>