【问题标题】:Return value of an Angular Directive and based on value have ng-if to display contentAngular 指令的返回值并基于值具有 ng-if 来显示内容
【发布时间】:2017-09-07 12:49:05
【问题描述】:

我有一个返回 0 或 1 的指令

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

所以data.valBit 将是 0 或 1

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

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

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

【问题讨论】:

  • ?这是什么意思

标签: javascript html angularjs angular-directive angular-controller


【解决方案1】:

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

示例:(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;
    }
  }
})

【讨论】:

猜你喜欢
  • 2016-03-15
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多