所以这是我重要的代码:

// app.js
$scope.buy = function(name, price) {
console.log(name, price);
$scope.cart[0].subtotal = $scope.cart[0].subtotal + price;
$scope.cart[0].items = $scope.cart[0].items + name + "\n"; // I tried using \n.
}


// index.html
<body ng-controller="StoreController">
<p>Search:</p><input type="text" ng-model="search" />
<input type="radio" name="order" ng-value="true" ng-model="reverse">$$$ - $</input>
<input type="radio" name="order" ng-value="false" ng-model="reverse">$ - $$$</input>
<hr>
<div ng-repeat="items in items | filter:search |orderBy:'price':reverse">
<img id="thumbnail" ng-src="{{ items.thumb }}" />
<h2>{{ items.name }}</h2>
<p>{{ items.price | currency}} </p>
<p>{{ items.desc }} </p>
<button ng-show="items.instock" ng-click="buy(items.name, items.price)">Buy {{items.name}}</button>
<p id="notStocked" ng-hide="items.instock">{{items.name}} is not in stock</p>
<hr>
</div>
<h1>Total Cart Price: {{ cart[0].subtotal }} </h1>
<p id="cartitems">Cart Items: {{ cart[0].items }}</p>
<button ng-show="subtotal > 0">Checkout</button>
</body>


我想使用buy()函数在cartitems段落中插入换行符,但找不到答案。

最佳答案

将名称推送到数组。然后在ng-repeat标记上使用<span>

JS

$scope.cart[0].purchases = [];
$scope.buy = function(name, price) {
    $scope.cart[0].subtotal += price;
    $scope.cart[0].purchases.push(name);
};


的HTML

<h1>Total Cart Price: {{ cart[0].subtotal }} </h1>
<p id="cartitems">Cart Items:
    <span ng-repeat="name in cart[0].purchases track by $index">
        {{name}}<br>
    </span>
</p>

关于javascript - 通过javascript函数将<br>插入段落,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34485975/

10-12 12:20
查看更多