作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个 Controller 和元素指令:
ngModule
.controller('summaryCtrl', [ '$scope', '$http', function($scope, $http){
$scope.loaded = false;
$http
.get('some/item/'+itemId) //how do I get this itemId
.success(function(data){
$scope.data = data;
$scope.loaded = true;
})
.error(function(data){
//TODO
});
}])
.directive('cpSummary', function(){
return {restrict: 'E', templateUrl: 'some/path.html'};
});
我想使用这样的指令:
<cp-summary item-id="{id}" ng-controller="summaryCtrl"></cp-summary>
item-id 属性由在 ng-repeat 中呈现 cp-summary 元素的父 Controller 设置。所以我只想知道是否有可能在 summaryCtrl
中获取 item-id
属性值。
最佳答案
你应该为你的指令使用一个 Controller 。然后您可以将 item-id
传递给指令 Controller 并执行这些操作。
app.directive('cpSummary', function(){
return {
restrict: 'E',
templateUrl: 'some/path.html',
scope: {
item_id: '=itemId'
},
controller: ['$scope','$http',function($scope,$http) {
$http
.get('some/item/'+$scope.item_id)
.success(function(data){
$scope.data = data;
$scope.loaded = true;
})
.error(function(data){
//TODO
});
}]
};
});
关于javascript - 如何将数据从自定义 Angular 元素传递到 Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31919095/
我是一名优秀的程序员,十分优秀!