Javascript 在AngularJS中获取选定选项

Javascript 在AngularJS中获取选定选项,javascript,angularjs,twitter-bootstrap,angularjs-ng-repeat,angular-ui-bootstrap,Javascript,Angularjs,Twitter Bootstrap,Angularjs Ng Repeat,Angular Ui Bootstrap,我正在生成一个照片的缩略图列表(使用ng repeat),在每个照片下我都有两个按钮,一个用于查看更多详细信息,另一个用于购买 我发现如何映射按钮有问题。基本上,我希望当用户在预订表单(这是一个不同的视图)中单击照片a下方的购买按钮时,他/她将看到与照片a相关的详细信息,而不是让用户从某个下拉列表中再次选择照片。照片列表来自一个JSON字符串 我发现的主要困难是如何将单击哪个按钮的详细信息传递到预订视图,以便我能够立即显示所选照片的详细信息 我是AngularJS的新手,不确定是否有简单的方法可

我正在生成一个照片的缩略图列表(使用ng repeat),在每个照片下我都有两个按钮,一个用于查看更多详细信息,另一个用于购买

我发现如何映射按钮有问题。基本上,我希望当用户在预订表单(这是一个不同的视图)中单击照片a下方的购买按钮时,他/她将看到与照片a相关的详细信息,而不是让用户从某个下拉列表中再次选择照片。照片列表来自一个JSON字符串

我发现的主要困难是如何将单击哪个按钮的详细信息传递到预订视图,以便我能够立即显示所选照片的详细信息

我是AngularJS的新手,不确定是否有简单的方法可以做到

我的HTML是这样的:

<div class="col-md-4 ng-scope" ng-repeat="photo in photos">
<div class="thumbnail">
  <img src="{{photo.thumbnail}}" alt="{{photo.title}}">
  <div class="caption">
    <h4 class="ng-binding">{{photo.title}}</h4>
    <p><button type="button" class="btn btn-default">Photographer</button><br ><button type="button" class="btn btn-default">Purchase</button></p>
  </div>
</div>
photosCtrl-JS:

angular
.module('app')
.controller('photosCtrl', ['$scope', 'photos', function($scope, photos) {
    $scope.photos = photos;
}]);

使用
ngClick

<p><button type="button" class="btn btn-default" ng-click="photo.showPhotographer()">Photographer</button><br ><button type="button" class="btn btn-default" ng-click="photo.buy()">Purchase</button></p>

使用
ngClick

<p><button type="button" class="btn btn-default" ng-click="photo.showPhotographer()">Photographer</button><br ><button type="button" class="btn btn-default" ng-click="photo.buy()">Purchase</button></p>

使用
ngClick

<p><button type="button" class="btn btn-default" ng-click="photo.showPhotographer()">Photographer</button><br ><button type="button" class="btn btn-default" ng-click="photo.buy()">Purchase</button></p>

使用
ngClick

<p><button type="button" class="btn btn-default" ng-click="photo.showPhotographer()">Photographer</button><br ><button type="button" class="btn btn-default" ng-click="photo.buy()">Purchase</button></p>

正如@Ashesh所建议的,使用
ngClick
指令是一个好主意

假设包含照片的JSON附带一组照片对象,我宁愿在
photosCtrl
的范围内添加两个函数,如下所示:

angular.module('app')
  .controller('photosCtrl', ['$scope', 'photos', function($scope, photos) {
    $scope.photos = photos;

    $scope.showDetailsOf = function(photo) {
        // photo argument is the actual object of which 'details' button was pressed
        // coming from the ngRepeat in your template example
        console.log('show details of ' + photo.title);
    }

    $scope.purchase = function(photo) {
        // same as above
        console.log('purchase ' + photo.title);
    }
}]);
<button type="button" class="btn btn-default" ng-click="showDetailsOf(photo)">Details</button>
<button type="button" class="btn btn-default" ng-click="purchase(photo)">Purchase</button>
模板应如下所示:

angular.module('app')
  .controller('photosCtrl', ['$scope', 'photos', function($scope, photos) {
    $scope.photos = photos;

    $scope.showDetailsOf = function(photo) {
        // photo argument is the actual object of which 'details' button was pressed
        // coming from the ngRepeat in your template example
        console.log('show details of ' + photo.title);
    }

    $scope.purchase = function(photo) {
        // same as above
        console.log('purchase ' + photo.title);
    }
}]);
<button type="button" class="btn btn-default" ng-click="showDetailsOf(photo)">Details</button>
<button type="button" class="btn btn-default" ng-click="purchase(photo)">Purchase</button>
详细信息
购买

此外,您可以将此逻辑分解为例如
photoService
,这样您的控制器就不会包含业务逻辑,因为您的控制器和服务都可以更容易地被测试覆盖,因为您将它们解耦。希望这有帮助。

正如@Ashesh所建议的,使用
ngClick
指令是一个好主意

假设包含照片的JSON附带一组照片对象,我宁愿在
photosCtrl
的范围内添加两个函数,如下所示:

angular.module('app')
  .controller('photosCtrl', ['$scope', 'photos', function($scope, photos) {
    $scope.photos = photos;

    $scope.showDetailsOf = function(photo) {
        // photo argument is the actual object of which 'details' button was pressed
        // coming from the ngRepeat in your template example
        console.log('show details of ' + photo.title);
    }

    $scope.purchase = function(photo) {
        // same as above
        console.log('purchase ' + photo.title);
    }
}]);
<button type="button" class="btn btn-default" ng-click="showDetailsOf(photo)">Details</button>
<button type="button" class="btn btn-default" ng-click="purchase(photo)">Purchase</button>
模板应如下所示:

angular.module('app')
  .controller('photosCtrl', ['$scope', 'photos', function($scope, photos) {
    $scope.photos = photos;

    $scope.showDetailsOf = function(photo) {
        // photo argument is the actual object of which 'details' button was pressed
        // coming from the ngRepeat in your template example
        console.log('show details of ' + photo.title);
    }

    $scope.purchase = function(photo) {
        // same as above
        console.log('purchase ' + photo.title);
    }
}]);
<button type="button" class="btn btn-default" ng-click="showDetailsOf(photo)">Details</button>
<button type="button" class="btn btn-default" ng-click="purchase(photo)">Purchase</button>
详细信息
购买

此外,您可以将此逻辑分解为例如
photoService
,这样您的控制器就不会包含业务逻辑,因为您的控制器和服务都可以更容易地被测试覆盖,因为您将它们解耦。希望这有帮助。

正如@Ashesh所建议的,使用
ngClick
指令是一个好主意

假设包含照片的JSON附带一组照片对象,我宁愿在
photosCtrl
的范围内添加两个函数,如下所示:

angular.module('app')
  .controller('photosCtrl', ['$scope', 'photos', function($scope, photos) {
    $scope.photos = photos;

    $scope.showDetailsOf = function(photo) {
        // photo argument is the actual object of which 'details' button was pressed
        // coming from the ngRepeat in your template example
        console.log('show details of ' + photo.title);
    }

    $scope.purchase = function(photo) {
        // same as above
        console.log('purchase ' + photo.title);
    }
}]);
<button type="button" class="btn btn-default" ng-click="showDetailsOf(photo)">Details</button>
<button type="button" class="btn btn-default" ng-click="purchase(photo)">Purchase</button>
模板应如下所示:

angular.module('app')
  .controller('photosCtrl', ['$scope', 'photos', function($scope, photos) {
    $scope.photos = photos;

    $scope.showDetailsOf = function(photo) {
        // photo argument is the actual object of which 'details' button was pressed
        // coming from the ngRepeat in your template example
        console.log('show details of ' + photo.title);
    }

    $scope.purchase = function(photo) {
        // same as above
        console.log('purchase ' + photo.title);
    }
}]);
<button type="button" class="btn btn-default" ng-click="showDetailsOf(photo)">Details</button>
<button type="button" class="btn btn-default" ng-click="purchase(photo)">Purchase</button>
详细信息
购买

此外,您可以将此逻辑分解为例如
photoService
,这样您的控制器就不会包含业务逻辑,因为您的控制器和服务都可以更容易地被测试覆盖,因为您将它们解耦。希望这有帮助。

正如@Ashesh所建议的,使用
ngClick
指令是一个好主意

假设包含照片的JSON附带一组照片对象,我宁愿在
photosCtrl
的范围内添加两个函数,如下所示:

angular.module('app')
  .controller('photosCtrl', ['$scope', 'photos', function($scope, photos) {
    $scope.photos = photos;

    $scope.showDetailsOf = function(photo) {
        // photo argument is the actual object of which 'details' button was pressed
        // coming from the ngRepeat in your template example
        console.log('show details of ' + photo.title);
    }

    $scope.purchase = function(photo) {
        // same as above
        console.log('purchase ' + photo.title);
    }
}]);
<button type="button" class="btn btn-default" ng-click="showDetailsOf(photo)">Details</button>
<button type="button" class="btn btn-default" ng-click="purchase(photo)">Purchase</button>
模板应如下所示:

angular.module('app')
  .controller('photosCtrl', ['$scope', 'photos', function($scope, photos) {
    $scope.photos = photos;

    $scope.showDetailsOf = function(photo) {
        // photo argument is the actual object of which 'details' button was pressed
        // coming from the ngRepeat in your template example
        console.log('show details of ' + photo.title);
    }

    $scope.purchase = function(photo) {
        // same as above
        console.log('purchase ' + photo.title);
    }
}]);
<button type="button" class="btn btn-default" ng-click="showDetailsOf(photo)">Details</button>
<button type="button" class="btn btn-default" ng-click="purchase(photo)">Purchase</button>
详细信息
购买


此外,您可以将此逻辑分解为例如
photoService
,这样您的控制器就不会包含业务逻辑,因为您的控制器和服务都可以更容易地被测试覆盖,因为您将它们解耦。希望这能有所帮助。

共享HTML和JS代码/在JSFIDDLERN中共享它不清楚您的问题。@NitishKumar基本上我正在生成照片缩略图的动态列表,每个缩略图下面有两个按钮,一个用于查看有关摄影师的信息,另一个用于购买照片。我的困难是如何链接按钮,以便当用户单击照片A的购买按钮时,例如,他将被带到购买表单(这是一个不同的视图)并在那里显示照片A的详细信息,用户无需从某个组合框中重新选择照片。@NitishKumar简单地说,我不知道如何传递所选索引,因此,我将在购买表单状态/视图中重新显示所选照片的信息。照片和购买的详细信息还有另外两个控制器?共享HTML&JS代码/在JsFiddlernot中共享。请不要澄清您的问题。@NitishKumar基本上我正在生成照片缩略图的动态列表,每个缩略图有2个它下面的按钮,一个用来查看关于摄影师的信息,另一个用来购买照片。我的困难是如何链接按钮,以便当用户单击照片A的购买按钮时,例如,他将被带到购买表单(这是一个不同的视图)并在那里显示照片A的详细信息,用户无需从某个组合框中重新选择照片。@NitishKumar简单地说,我不知道如何传递所选索引,因此,我将在购买表单状态/视图中重新显示所选照片的信息。照片和购买的详细信息还有另外两个控制器?共享HTML&JS代码/在JsFiddlernot中共享。请不要澄清您的问题。@NitishKumar基本上我正在生成照片缩略图的动态列表,每个缩略图有2个它下面的按钮,一个用来查看关于摄影师的信息,另一个用来购买照片。我的困难是如何链接按钮,以便当用户单击照片A的购买按钮时,例如,他将被带到购买表单(这是一个不同的视图)并在那里显示照片A的详细信息,用户不必从某个组合框中重新选择照片。@NitishKumar简单地说,我不知道如何传递所选的索引类型,以便在“购买表单状态/视图”中重新显示所选照片的信息。还有两个其他控制器用于了解照片和购买的详细信息?共享HTML&