简体   繁体   中英

How to use ng-disabled with jade templates

Now I work with nodejs and investigate angularjs for fun) But I have some problem I want to add file upload for my blog project and i did it) So I want show upload button only if user select some file. It is not a problem with jQuery but I want to know how to do it with angular. So here I have more problems

Here is Jade template

form(method="post",enctype="multipart/form-data", action="/imageUpload")
              label Select image 
              input(type='file',name='image',onchange="angular.element($(this)).scope().inputChange()")
              button.btn.btn-primary(type='submit,ng-disabled='{{isEnable}})#uploadBtn Upload Image 

here is my work jQueryCod for validate

function UserController($scope)
{
    var sizeInput = $("input[type='file']").val().length;
    isEnable = sizeInput === 0 ;
    $( "#uploadBtn" ).toggle( isEnable )   
    $scope.inputChange = function(){
        sizeInput = $("input[type='file']").val().length;
        isEnable = !(sizeInput > 0);
        $( "#uploadBtn" ).toggle( isEnable );
    }
}

Of cource I can use only jquery - but I want to know how angular works.

So I want to use ng-disbled - but it doesn't work correctly. At first load it takes position true - and it all - it doesn't change

function UserController($scope)
{       
    var sizeInput = $("input[type='file']").val().length;
    $scope.isEnable = sizeInput === 0 ;       
    $scope.inputChange = function(){
        sizeInput = $("input[type='file']").val().length;
        myScope.isEnable = !(sizeInput > 0);

    }
}

I have found answer that here we mast use not {{ isEnable}} but just ng-disabled='isEnable' without parentheses but how inject it to jade???

Remove the {{}} since you're already writing in Angular inside ng-* attributes:

ng-disabled='{{isEnable}}

To:

ng-disabled='isEnable'

Use ng-show

button.btn.btn-primary(type='submit,ng-show='isEnable')#uploadBtn Upload Image

This will tell angular to show your button only when isEnable is true. When it's false, the button won't be shown.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM