Return Angular 指令的值和基于值的 ng-if 来显示内容

Return value of an Angular Directive and based on value have ng-if to display content

我有一个指令 returns 0 或 1

 <my-code inputcode="{{ data.valBit }}"></my-code>

所以 data.valBit 将是 0 或 1

如果它是 1 ,那么我希望它显示一段 HTML 代码,我该怎么做?从指令返回所有 html 的问题是它与 ngmessage 验证一起放入了一个表单,因此传递表单或混淆 link 或编译器

并不有趣
<div ng-if="data.valBit == 1">
    part of my form,  with ngmessage etc...   $error  etc... 
    .......
</div>

是否可以从我可以评估并决定是否显示或隐藏 html 的一部分的指令中获得回调?

最简单的方法是利用双向数据绑定
在您的情况下,问题在于您将评估值而不是变量传递给指令。
只需删除指令参数中的大括号 <my-code inputcode="data.valBit"></my-code>.

示例:(PLUNKER)

HTML

<my-code inputcode="data.valBit"></my-code>
<p ng-if="data.valBit == 1">
  part of my form,  with ngmessage etc... $erroretc... 
</p>

指令

app.directive('myCode', function() {
  return {
    restrict: 'E',
    scope: {
      inputcode: "=" //two way binding
    },
    link: function(scope, element, attrs){
      scope.inputcode = 1;
    }
  }
})