如何将电流范围以角度传递给控制器函数?

时间:2021-12-08 15:11:47

I am trying to do something like below:

我试着做如下的事情:

  <ul>
       <li ng-click="GetBookDetails({{book.id}})" ng-repeat="book in books">{{book.Title}}</li>                        
  </ul>

Below is the controller:

下面是控制器:

 angular.module('app')
.controller("Books", ["$scope", "$http", function ($scope, $http) {

    $http.get(url).success(function (data) {
        $scope.books = data.items;
    })

    $scope.GetBookDetails = function (bookId) {
        $http.get(url + '/' + bookId).success(function (data) {
            $scope.bookDetails = data;
        })
    }
}]);

But ng-click="GetBookDetails({{book.id}})" this is giving invalid key error. Could you please help me out?

但是,ng-click=“GetBookDetails({{{{book.id})”会产生无效的键错误。你能帮我一下吗?

2 个解决方案

#1


2  

Try ng-click="GetBookDetails(book.id)"

尝试ng-click = " GetBookDetails(book.id)”

#2


0  

ng-click="GetBookDetails(book.id)"

You must not use interpolation in an angular expression.

你不能在角度表达式中使用插值。

You should also respect the naming conventions and rename your function to getBookDetails(), and the Title attribute to title. You'll lose much time and introduce bugs if you have to wonder, every time you use a function or an attribute, if it starts with a lowercase letter or not.

您还应该遵守命名约定,将函数重命名为getBookDetails(),并将Title属性重命名为Title。如果你想知道,每次你使用一个函数或一个属性,如果它以小写字母开头,你将会损失很多时间并引入错误。

Finally, success() and error() callbacks on $http promises are deprecated. You should use then(), as for any other promise:

最后,成功()和错误()对$http承诺的回调被弃用。你应该使用then(),就像其他承诺一样:

$http.get(url).then(function (response) {
    $scope.books = response.data.items;
})

#1


2  

Try ng-click="GetBookDetails(book.id)"

尝试ng-click = " GetBookDetails(book.id)”

#2


0  

ng-click="GetBookDetails(book.id)"

You must not use interpolation in an angular expression.

你不能在角度表达式中使用插值。

You should also respect the naming conventions and rename your function to getBookDetails(), and the Title attribute to title. You'll lose much time and introduce bugs if you have to wonder, every time you use a function or an attribute, if it starts with a lowercase letter or not.

您还应该遵守命名约定,将函数重命名为getBookDetails(),并将Title属性重命名为Title。如果你想知道,每次你使用一个函数或一个属性,如果它以小写字母开头,你将会损失很多时间并引入错误。

Finally, success() and error() callbacks on $http promises are deprecated. You should use then(), as for any other promise:

最后,成功()和错误()对$http承诺的回调被弃用。你应该使用then(),就像其他承诺一样:

$http.get(url).then(function (response) {
    $scope.books = response.data.items;
})