<html>
<head>
<title>
</title>
<script src="../../angular.min.js"></script>
<script type="text/javascript">
myApp = angular.module('myApp', []);
myApp.controller("empController", function($scope){
$scope.ename = {nm : "Harry"};
$scope.changeName = function(){$scope.ename.nm = "Ron";};
});
myApp.directive("empDirective", function(){
return {
scope : {employeeName : "=myEmpName", nameChange : "&click"},
template : 'Employee Name is {{employeeName.nm}}. <button ng-click="nameChange()">Change Name</button>'
};
});
</script>
</head>
<body ng-app="myApp">
<div ng-controller="empController">
<div emp-directive myEmpName="ename" click="changeName()" > </div>
{{ename.nm}}
</div>
</body>
当我运行上面的代码时,我没有在指令中获得employeeName.nm值。不确定我错过了什么。我是AngularJS的新手
答案 0 :(得分:2)
在模板中,更改
myEmpName
到
my-emp-name
Angular要求模板中的连字符分隔属性,并通过称为规范化的过程将它们转换为驼峰案例。