AngularJS:插值属性

时间:2014-05-13 08:05:08

标签: javascript angularjs angularjs-directive angularjs-compile

说我有以下两个指令:

angular.module('foo').directive('outer', [function(){
  return {
    restrict: 'E',
    scope: {
      inner: '@',
      innneParams: '@'
    },
    template: "<div {{inner}}{{innerParams}}></div>",
    link: function(scope, elem, attrs){
      console.debug("I AM IN YOUR OUTER DIRECTIVE PASSING YOUR D00DZ!")
    }
  }

}]);
angular.module('foo').directive('innerDir', [function(){
  return {
    restrict: 'EA',
    scope: {
      innerParam: '='
    },
    template: "<div>{{massagedInner}}</div>",
    link: function(scope, elem, attrs){
      console.debug('I AM IN YOUR INNER DIRECTIVE MASSAGING YOUR D00DS!')
      scope.massagedInner = scope.innerParam + "FROM YOUR DOGE!"
    }
  }
}]);

以下HTML:

<outer inner="inner-dir" my-awesome-scope-value="myAwesomeScopeValue" inner-params="inner-param='myAwesomeScopeValue'"></outer>

外部指令控制台调试触发器,内部调试器没有。对我来说,有什么好方法可以实现这种行为吗?

普兰克:http://plnkr.co/edit/jXbtWvYvtFXCTWiIZUDW?p=preview

1 个答案:

答案 0 :(得分:1)

有很多事情你做错了。我已经做出了我认为接近您希望它做的更改,您可以从这里更改代码。

Here's a working version

这就是script.js现在的样子;

angular.module('foo', []);
angular.module('foo').controller('fooController', ['$scope', function(scope){
  scope.myAwesomeScopeValue = 'O HAI THERE'  
}]);
angular.module('foo').directive('outer', [function(){
  return {
    restrict: 'E',
    scope: {
      // inner: '@',
      // innnerParams: '@'
      innerParam: '@'
    },
    template: "<div inner {{inner}} {{inner-param}}></div>",
    link: function (scope) {
      console.log('OUTER', scope.innerParam);
    }
  }

}]);
angular.module('foo').directive('inner', [function(){
  return {
    restrict: 'A',
    // scope: {
    //   innerParam: '='
    // },
    replace: false,
    template: "<div>{{massagedInner}}</div>",
    link: function(scope, elem, attrs){
      scope.massagedInner = scope.innerParam + "FROM YOUR DOGE!"
      console.log('INNER');
    }
  }
}]);

为了简洁起见,我已经将你的一些行注释掉了。我希望这会有所帮助。