ng-show不在指令中工作

时间:2014-09-12 08:09:45

标签: angularjs angularjs-directive ng-show

我无法在我的纺纱元素上使用ng-show来工作。对于指令,我有一个孤立的范围,但由于它是我内部的一个元素,我认为它应该有用吗?

任何人都知道我做错了什么?

代码段:

angular.module('lr.upload.directives').directive('uploadButton', [
    'upload',
    function(upload) {
        return {
            restrict: 'EA',
            scope: {
                data: '=?data',
                url: '@',
                param: '@',
                method: '@',
                onUpload: '&',
                onSuccess: '&',
                onUploadTest: '&',
                onError: '&',
                onComplete: '&'
            },
            link: function(scope, element, attr) {
                scope.uploading=false;
                var el = angular.element(element);
                var fileInput = angular.element('<input type="file" />');
                var spinningElement = angular.element('<img ng-show="uploading" class="uploadSpinner" src="../../img/loading.gif" />');
                el.append(fileInput);
                el.append(spinningElement);

解决方案:

angular.module('lr.upload.directives').directive('uploadButton', [
    'upload', '$compile',
    function(upload, $compile) {
        return {
            restrict: 'EA',
            scope: {
                data: '=?data',
                url: '@',
                param: '@',
                method: '@',
                onUpload: '&',
                onSuccess: '&',
                onUploadTest: '&',
                onError: '&',
                onComplete: '&'
            },
            link: function(scope, element, attr) {
                scope.uploading = false;
                var el = angular.element(element);
                var fileInput = angular.element('<input type="file" />');
               var spinningElement = $compile('<img ng-show="uploading" class="uploadSpinner" src="../../img/loading.gif" />')(scope);
                el.append(fileInput);
                el.append(spinningElement);

1 个答案:

答案 0 :(得分:1)

我认为您应该使用$ compile来使其正常工作

var spinningElement = $compile('<img ng-show="uploading" class="uploadSpinner" 
src="../../img/loading.gif" />')(scope);