为什么我的指令没有显示我传递给它的字符串

时间:2014-05-13 17:36:27

标签: javascript angularjs angularjs-directive

我正在尝试在Angular中构建我的第二个指令。我正面临一个让我发疯的问题。我是初学者,试图研究它,跟随this very useful explanationthis great tutorial,但我不明白为什么,我不能将简单的字符串传递给指令。

这是我的超级简单指令的代码:

uxctModule.directive('postIt', function() {
  return {
      restrict: 'AE',
      replace: 'true',
      scope:{
         message: "@"
      },

      template:"<div>Received: {{message}}</div>"
  };
});

我正在尝试将一个非常简单的字符串传递给它。

这是我的标记:

 <div post-it message='{{postItContent}}'></div>

其中postItComment是来自其控制器的范围变量:

 $scope.postItContent = "THIS IS A STRING DECLARED IN MY CONTROLLER";

发生了什么事?有人可以给我一个解释吗?

1 个答案:

答案 0 :(得分:1)

请参阅此JS小提琴,您的解决方案可以正常运行:http://jsfiddle.net/ahchurch/S6dBE/1/

除了你的问题之外什么都不是(不确定它是否是一个错字或你还没注意到) - $ scope.postItContent不是你绑定你的消息。你的约束力是{{postItComment}}。

var myApp = angular.module('myApp',[]);

myApp.directive('postIt', function() {
  return {
      restrict: 'AE',
      replace: 'true',
      scope:{
         message: "@"
      },

      template:"<div>Received: {{message}}</div>"
  };
});
//myApp.directive('myDirective', function() {});
//myApp.factory('myService', function() {});

function MyCtrl($scope) {
    $scope.message = 'Superhero';
}