0
我是AngularJS的新手。 我想構建一個可以顯示書籍信息的應用程序,用戶可以點擊一個圖標來刪除書籍信息。我使用顯示每本圖書信息的客戶指令。圖書信息列表上有一個圖標,用戶可以點擊圖標刪除圖書信息。指令中的呼叫控制器功能
我在指令中的圖標上有一個ng單擊偵聽器,但我不知道如何讓它調用控制器中的函數define。
你能告訴我該怎麼做嗎?有沒有更好的方法來實現它?
感謝
HTML代碼:
<div class="container" ng-app="myApp" ng-controller="bookCtrl">
<div ng-repeat="book in books | filter :{'name' : bookName}" book-List="book"></div>
</div>
app.js:
var app = angular.module('myApp',[]);
app.controller('bookCtrl', function ($scope,$http){
$scope.books=[];
$http.get("products.json")
.then(function(response){
$scope.books = response.data;
});
$scope.removeItem = function(x){
$scope.index = x;
$scope.books.splice(x,1);
}
});
app.directive('bookList', function(){
return{
restrict: 'EA',
scope: { item : '=bookList'},
templateUrl: "template.html",
};
});
模板:
<div class="jumbotron" ng-app="myApp" ng-controller="bookCtrl">
<div class="row" >
<div class="col-md-7">
<h3>{{item.name}}</h3>
</div>
<div class="col-md-2">
<button type="button" class="btn btn-primary">{{item.price}}</button>
</div>
<div class="col-md-2">
<span class="glyphicon glyphicon-remove-sign" ng-click="removeItem($Index)"></span>
</div>
</div>
<div>{{item.description}}</div>
</div>