javascript - 将 JSON 对象传递给 Angular Directive(指令)

标签 javascript angularjs json angularjs-directive pug

我正在尝试将 json 对象传递给 angular 指令,但还找不到任何解决方案。

这是代码。

指令

 function agEcalendar() {
    var directive = {
      restrict: 'E',
      templateUrl: 'app/components/ecalendar/ecalendar.html',
      scope: {
          event: '@event'
      },
      controller: EcalendarController,
      controllerAs: 'vm',
      bindToController: true
    };

    return directive;
  }

我在这里调用jade中的指令

ag-ecalendar(event='{{vm.calendar.event}}')

最佳答案

使用 < 传递对象(新的单向绑定(bind)选项)这将使您能够将对象传递给指令隔离范围。

scope: {
    event: '<event'
},

HTML

ag-ecalendar(event='vm.calendar.event')

在当前情况下,它通过 @ 传递只不过是将对象值转换为 string .

Note The suggested way would work for Angular 1.5+ version, otherwise you need to make it as event: '=event'(eventually that will enable two way binding)

关于javascript - 将 JSON 对象传递给 Angular Directive(指令),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37302638/

相关文章:

javascript - 将一个数分解为 100 的因数并保留余数

javascript - 如何在不重新评估表达式的情况下从 ng-show 属性获取 bool 值?

angularjs - Angular Material sidenav 事件广播

javascript - 什么正则表达式可以替换所有其他单词?

javascript - 无法读取未定义的 React-Native Firebase React-native-fetch-blob 的属性 'DocumentDir'

IE8 上的 Javascript FileReader

javascript - JSON 输出到 AngularJs

php - org.json.JSONException : Value <html of type java. lang.String 无法转换为 JSONObject

python - 在 Elasticsearch 中添加稀疏向量作为字段

json - Shopify - 从 Liquid 模板中的外部 REST API 加载数据