自定义指令中的angular指令(ng-class)

时间:2013-11-06 06:20:07

标签: angularjs angularjs-directive

我只是想在每次用户点击链接时显示和隐藏一个微调器,在这种情况下我使用一个承诺来包装实际的click方法,当promise得到解决时我想隐藏微调器。

由于某种原因,似乎指令没有将我在范围内的值与模板中的ng-class绑定

任何想法?

   app.directive('toogleTextLink', function($compile,$q) {
    return {
        restrict: 'AE',
        scope: { callback: "&targetMethod" },
        template: '<div><a style="cursor: pointer" ><b>{{text}}</b></a> <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.$apply();
                if (scope.callback) {

                    var deferred = $q.defer();
                    deferred.resolve(scope.callback());
                    deferred.promise.then(function () {
                        scope.show = false;
                        console.log("then called");
                    });
                }

            });
        }
    };
});

查看plunker

1 个答案:

答案 0 :(得分:0)

这个问题会影响微调器(CSS)的大小,你也会立即解决这个问题,所以你将无法看到微调器。

检查这些更改 http://plnkr.co/edit/viwzCb?p=preview