角度js中的自定义指令

时间:2014-08-01 11:53:32

标签: javascript html5 angularjs

为什么angular js中的自定义指令对我不起作用。我只获取用户:但是没有获取我在userinfo自定义指令标记中传递的用户名

user_directive_index.html文件:

<html ng-app='app' >
<head>
</head>
<title>
User Directive
</title>
<body >

<div>
<div ng-model='firstcontroller'>
<userinfo user="Raj"></userinfo>
</br>
<userinfo user="Sudhir"></userinfo>
</div>
</div>
<script type="text/javascript" src="../js/angular.min.js"></script>
<script src='app.js'></script>
</body>
</html>

App.js文件

var app1=angular.module('app',[]);
app1.controller('firstcontroller',function($scope){
$scope.Raj={};
$scope.Raj.firstname='Rajeshwar';
$scope.Raj.lastname='Bose';
$scope.Sudhir={};
$scope.Sudhir.firstname='Sudhir';
$scope.Sudhir.lastname='Ranjan';

});

app1.directive('userinfo',function(){
var directive={};

directive.restrict='E';
directive.template='User:{{user.firstname}} {{user.lastname}}';

directive.scope={
user:"= user"
};

return directive;
});

我的代码来自:http://tutorials.jenkov.com/angularjs/custom-directives.html

1 个答案:

答案 0 :(得分:2)

您必须在此处更改指令ngModel

<div ng-model='firstcontroller'>
        ^^^^^

ngController

<div ng-controller='firstcontroller'>