简体   繁体   English

AngularJS指令显示/隐藏两个模板

[英]AngularJS directive to display/hide either templates

I have a checkbox as follows, and I want to show/hide taxid input or chkno input when it is checked or not. 我有一个复选框,如下所示,我想在选中或不显示时隐藏/隐藏滑行输入或chkno输入。

<input type="checkbox" id="chks" ng-model='chks' chk-no>

Inorder to do this I have written a directive as follows, but it isn't working. 为了做到这一点,我编写了如下指令,但是它不起作用。 what exactly is the issue in my directive? 我的指令中到底是什么问题?

app.directive('chkNo', function($compile,$log)
{
    var template_taxid = "<label id='taxidlbl'>Tax ID Number</label><br/><input type=text' id='taxid' name='taxid' placeholder='Tax ID Number' ng-model='taxid' required>";
    var template_chkno = "<label id='chklbl'>Check Number</label><br/><input type='text' id='chn' placeholder='Checking Number' ng-model='chn' required>";

    var getTemplate = function(chks)
    {
        var template = '';
        if(chks){
            template = template_taxid;
        }
        else
        {
            template = template_chkno;
        }
    }
    var linker = function(scope,element, attrs)
    {
        element.html(getTemplate(scope.chks)).show();
        $compile(element.contents())(scope);
    }
    return{
        restrict    : 'EA',
        replace     : true,
        link        : linker
    }
});

you can just use ng-show ng-hide 你可以只用ng-show ng-hide

<input type="checkbox" ng-model='chks'>

<div ng-show"chks">
    <label >Tax ID Number</label>
    <br/>
    <input type=text' name='taxid' placeholder='Tax ID Number' ng-model='taxid' required>"
</div>
<div ng-hide"chks">
    <label>Check Number</label>
    <br/>
    <input type='text' placeholder='Checking Number' ng-model='chn' required>
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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