angular.js - angular实现捕获选中项id并将此id作为参数传递给跳转的页面
怪我咯
怪我咯 2017-05-15 17:04:29
0
3
770

在指令中使用ng-repeat循环显示出商品列表

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>'

        };
    });

在controller中定义跳转传参,main为产品列表展示页,design为选中跳转的商品详情页

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);
})

此时并不跳转,也不能触发点击事件,将在ng-repeat中添加的ng-click改为toPage(id)可以跳转,但此时传递的参数依然是controller中写死的$scope.data[0].id,不是点击对应的商品
请问各位大神有什么解决办法嘛?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
滿天的星座

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

阿神
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});
    }
phpcn_u1582

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!