我正在尝试在Angular中构建我的第二个指令。我正面临一个让我发疯的问题。我是初学者,试图研究它,跟随this very useful explanation和this 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";
发生了什么事?有人可以给我一个解释吗?
答案 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';
}