search

Home  >  Q&A  >  body text

angular.js - Angular implements capturing the selected item id and passing this id as a parameter to the jumped page

Use ng-repeat in the command to loop and display the product list

angular.module("goodsInsert",[])
    .directive("myDirective",function(){
        return{
            restrict:'EA',
            template:'<p class="goods_wrap"  ng-repeat="item in data" ng-click="toPage({{item.id}})">'
            +'<p class="goods_left"><img src="{{item.images}}" alt="" /></p>'
            +'<p class="goods_right">'
            +'<p class="goods_describe">{{item.name}}</p>'
            +'<p class="goods_info"><p>{{item.price | currency:"¥"}}</p><p>已售{{item.saleCount}}件</p></p>'
            +'<p class="user_wrap"><img src="{{item.producer.header}}" alt="" class="user"/><p class="grey">{{item.producer.lastname}}</p></p>'
            +'</p>'
            +'</p>'

        };
    });

Define the jump parameters in the controller, main is the product list display page, design is the product details page selected for jump

myApp.controller("mainCtrl", function($scope,$http,goodsService,$state) {
    var data=goodsService.getNewGoods();
    data.then(function(data){
        $scope.data=data.results;
        
    },function(data){
        $scope.error=data;
    });
    $scope.toPage=function(id){
        if ($scope.$root.$$phase != '$apply' && $scope.$root.$$phase != '$digest') {
    $scope.$apply();
}
        alert(111);
        $state.go('index.detail',{ID:$scope.data[0].id});
    }
});
myApp.controller('designCtrl',function($scope,$state,$stateParams){
    
    var receivedId = $stateParams.ID;
    alert(receivedId);
})

There is no jump at this time, and the click event cannot be triggered. Changing the ng-click added in ng-repeat to toPage(id) can jump, but the parameters passed at this time are still hard-coded in the controller. $scope.data[0].id, instead of clicking on the corresponding product
Do you have any solutions?

怪我咯怪我咯2759 days ago787

reply all(3)I'll reply

  • 滿天的星座

    滿天的星座2017-05-15 17:06:29

    I found the answer! !
    ng-click=toPage(item.id)
    Remove {{}} in the parameters and leave everything else alone, it will be solved

    reply
    0
  • 阿神

    阿神2017-05-15 17:06:29

    angular.module("goodsInsert",[])
        .directive("myDirective",function(){
            return{
                restrict:'EA',
                template:'<p class="goods_wrap"  ng-repeat="(item,key) in data" ng-click="toPage(key)">'
                +'<p class="goods_left"><img src="{{item.images}}" alt="" /></p>'
                +'<p class="goods_right">'
                +'<p class="goods_describe">{{item.name}}</p>'
                +'<p class="goods_info"><p>{{item.price | currency:"¥"}}</p><p>已售{{item.saleCount}}件</p></p>'
                +'<p class="user_wrap"><img src="{{item.producer.header}}" alt="" class="user"/><p class="grey">{{item.producer.lastname}}</p></p>'
                +'</p>'
                +'</p>'
    
            };
        });
    $scope.toPage=function(key){
            if ($scope.$root.$$phase != '$apply' && $scope.$root.$$phase != '$digest') {
        $scope.$apply();
    }
            alert(111);
            $state.go('index.detail',{ID:$scope.data[key].id});
        }

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-15 17:06:29

    Uh-huh. The method in ng-click passes parameters without {{}}

    reply
    0
  • Cancelreply