Javascript AngularJS:ng指令ng transclude中的ng重复
嗨,我是AngularJS的新手,所以如果这是一个简单的问题,请原谅我。 我有一个简单的指令,看起来像这样:Javascript AngularJS:ng指令ng transclude中的ng重复,javascript,angularjs,angularjs-directive,angularjs-ng-repeat,transclusion,Javascript,Angularjs,Angularjs Directive,Angularjs Ng Repeat,Transclusion,嗨,我是AngularJS的新手,所以如果这是一个简单的问题,请原谅我。 我有一个简单的指令,看起来像这样: app.directive('myDirective', function() { return { restrict: 'E', transclude: true, replace: true, template: '<div ng-transclude></div>' }; });
app.directive('myDirective', function() {
return {
restrict: 'E',
transclude: true,
replace: true,
template: '<div ng-transclude></div>'
};
});
<my-directive>
<div ng-repeat="item in data">{{item.name}}</div>
</my-directive>
控制器和指令之间可能存在范围可见性问题 看看
var myModule=angular.module('ui.directives',[]);
myModule.directive('myDirective',function(){
返回{
限制:'E',
是的,
替换:正确,
模板:“”
};
});
myModule.controller('myController',函数($scope){
$scope.data=[
{name:'foo'},
{name:'bar'}
];
});
angular.module('myApp',['ui.directives']);
数据来自我的控制器。自定义指令嵌套在属性为ng controller=“MyController”的div中。我使用$http获取数据,但为了简化,我的控制器现在只包含$scope.data=[{'name':'Foobar'}];请分享您的控制器。您是对的,这肯定是一个范围可见性问题。这帮了大忙。谢谢
<my-directive>
<!-- ngRepeat: item in data -->
</my-directive>
app.controller('MyController', ['$scope', function($scope) {
$scope.data = [{'name': 'Foobar'}];
}]);
var myModule = angular.module('ui.directives', []);
myModule.directive('myDirective', function() {
return {
restrict: 'E',
transclude: true,
replace: true,
template: '<div ng-transclude></div>'
};
});
myModule.controller('myController', function($scope){
$scope.data = [
{name: 'foo'},
{name: 'bar'}
];
});
angular.module('myApp', ['ui.directives']);