javascript - AngularJS : directive scope

标签 javascript angularjs angularjs-directive

我正在尝试在指令的范围内设置变量:

HTML:

<div personalize-product>
        Owned: {{ isOwned }}    
</div>

咖啡:

angular.module('yoApp').directive 'personalizeProduct', ->
    restrict: 'A'
    link: (scope, el, attrs) ->
        scope.isOwned = 'maybe'

但是它不起作用。我显然错过了一些东西,但我无法在 Angular 文档中找到答案。

编辑:我应该提到 - 我不想将 isOwned 传递给指令,我希望指令负责设置它。

最佳答案

这是工作代码:

HTML:

  <body ng-app="yoApp">
    <div personalize-product product-id="6">
    </div>

    <div personalize-product product-id="8">
    </div>
  </body>

JS:

angular.module('yoApp', [])
  .directive('personalizeProduct', function() {
    return {
      restrict: 'A',
      scope: {},
      link: function(scope, el, attrs) {
        scope.isOwned = attrs.productId;
      },
      template: '<div>{{isOwned}}</div>'
    }
  })

关于javascript - AngularJS : directive scope,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23610732/

相关文章:

javascript - sails.js 使用 async.js 查找每个并行调用的查询 - 每个都提前返回

javascript - 可以使用 target 和 window.name 吗?

javascript - 单击 div 而不是文件输入标签时打开文件选择

javascript - Angular 观察指令中 $http.pendingRequests 的变化

javascript - 为什么我得到的是 [object Object] 而不是 JSON?

javascript - 如何使用jQuery根据选择的条目改变界面

javascript - Angular Bootstrap 模态未显示

angularjs - 使用angularjs在剑道调度程序上自定义删除确认弹出窗口

javascript - 内容可编辑指令无法正常工作

angularjs - 更新缓存的模板