Angular指令:从指令到范围的多个值

pkb*_*lin 3 directive angularjs angularjs-directive

我想从属性中将多个值从指令公开给$ scope.指令是动态生成的,如下所示:

<my-directive first-value="foo" second-value="bar" third-value="foobar"></my-directive>
Run Code Online (Sandbox Code Playgroud)

我需要$ scope中的值来将它们提供给模板并使用它们.

Mar*_*coS 5

简单... :-)

var app = angular.module('app', []);
app.controller('myCtrl', function ($scope) {});
app.directive('myDirective', function() {
  return {
    restrict: 'E',
    template: '<p>myDirective:</p>{{firstValue}}, {{secondValue}}, {{thirdValue}}',
    scope: {
      firstValue: '@',
      secondValue: '@',
      thirdValue: '@'
    },
  }
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
  <div ng-controller="myCtrl">
    <my-directive first-value="foo" second-value="bar" third-value="foobar"></my-directive>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

但你真的应该尝试自己编写这种代码,下次...... :-)