angularJS如何在指令的链接中更改attrs

nat*_*ila 2 javascript jquery angularjs angularjs-directive

我在我的应用程序中设置了一个进度
我想要控制角度指令的进度
但是如何更改data-valuedata-total指令的链接功能?

app.html

    <div class="ui indicating small progress" data-value="39" data-total="50" plan-progress>
      <div class="bar">
        <div class="progress"></div>
      </div>
    </div>
Run Code Online (Sandbox Code Playgroud)

在这个HTML中,我想要改变data-valuedata-total

我试试这个:

app.js

  todoApp.directive('planProgress', function() {
    return {
      link: function(scope, elem, attrs) {
        attrs.value = 10
        attrs.total = 20
        elem.progress();
      }
    };
  });
Run Code Online (Sandbox Code Playgroud)

但它没有用,
所以我想知道如何在我的指令中改变它?

Md.*_*que 6

使用attrs.$set()在您的链接功能和重新编译的元素.另外,不要忘记将$compile服务注入您的指令.在您的html中,您已将指令添加为属性,但未在指令定义中的restrict值中提及它.您需要在指令定义中提及它.请参阅下面的代码:

todoApp.directive('planProgress', function($compile) {
    return {
      restrict: 'A',
      link: function(scope, elem, attrs) {
        attrs.$set('value', 10);
        attrs.$set('total', 20);
        $compile(elem)(scope);
      }
    };
  });
Run Code Online (Sandbox Code Playgroud)