我正在尝试在指令中设置范围并将其打印出来。但它给出了错误。有人可以帮忙吗?
//module declaration
var app = angular.module('myApp',[]);
//controller declaration
app.controller(function($scope){
$scope.name = "Joseph";
});
//app declaration
app.directive('myStudent',function(){
return{
template: "Hi! Dear!! {{name}}"
}
});

<body ng-app="myApp" ng-controller="myCtrl">
<my-student></my-student>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
</body>
&#13;
答案 0 :(得分:2)
您的控制器声明不完整
//controller declaration
app.controller('myCtrl', function($scope){
$scope.name = "Joseph";
});
答案 1 :(得分:0)
您需要通过;
正确声明您的控制器app.controller("myCtrl", ["$scope", function($scope){
$scope.name = "Joseph";
}]);
你需要告诉你的指令通过;
使用这个控制器app.directive('myStudent',function(){
return{
template: "Hi! Dear!! {{name}}",
controller: "myCtrl"
}
});
答案 2 :(得分:0)
<body ng-app="myApp" ng-controller="myCtrl">
<my-student></my-student>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
</body>
app.controller("myCtrl", ["$scope", function($scope){
$scope.name="Adrian":
}]);
指令中的
app.directive('myStudent',function(){
return{
scope:'=',
template: "Hi! Dear!! {{name}}"
})
或
<body ng-app="myApp" ng-controller="myCtrl">
<my-student name="name"></my-student>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.5/angular.min.js"></script>
</body>
和你的指令:
app.directive('myStudent',function(){
return{
scope:{
name:'=',
},
template: "Hi! Dear!! {{name}}"
})