单击角度js动态创建json

时间:2017-07-16 10:42:27

标签: javascript angularjs json

我正在尝试以下面的格式动态创建购物车部分的JSON,但在我的代码中却没有创建。

$scope.cart2 = [{
    "image": $rootScope.img,
    "login": $rootScope.login
}, {
    "image": $rootScope.img,
    "login": $rootScope.login
}];

我的代码: -

x.controller("first", function($scope, $http, $rootScope) {
    $scope.cart1 = function(x, y) {
        console.log(x);
        $rootScope.img = x;
        $rootScope.login = y;
    }
});

x.controller("cart", function($scope, $rootScope) {
    $scope.getData = function() {
        $scope.cart2 = [];
        $scope.addd = {
            "image": $rootScope.img,
            "login": $rootScope.login
        };
        $scope.cart2.push($scope.addd);
        console.log($scope.cart2)
    };
});

我的HTML: -

<div class="cartadd pull-left">
    <a href="javascript:void(0)" ng-click="cart1(x.avatar_url,x.login)">Cart</a>
</div>

0 个答案:

没有答案