Angularjs Button loading state directive with ng-disabled directive

大兔子大兔子 提交于 2019-12-04 18:49:32

You can listen to the ng-disabled property in the parent's scope, and if it is disabled then just simply does nothing.

The trick is to watch on ngdisabled property like this

scope.$watch(attrs.ngDisabled, function (newVal) {...

I want to shed some light on since I can't test your code without other pieces, you probably can do something like this:

.directive("btnLoading", function () {
    return function (scope, element, attrs) {

        //maybe you need just scope.$watch instead of scope.$parent.$watch. Depends on your impl.
        scope.$parent.$watch(attrs.ngDisabled, function (newVal) {

            //you can make the following line more robust
            if (newVal === 'disabled' || newVal === 'true') return;

            function () {
                return scope.$eval(attrs.btnLoading);
            },

            function (loading) {
                if (loading) return element.button("loading");
                element.button("reset");
            }
        });
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!