Angular學(xué)習(xí)第四天

今天我們來做一下電商網(wǎng)站常見的購物車功能

實現(xiàn)功能,購物車相信大家都熟悉

  • 顯示商品列表
  • 商品添加進購物車
  • 購物車內(nèi)沒有商品時,顯示去購物
  • 當(dāng)購物車內(nèi)沒有商品時,顯示去購物
  • 購物車內(nèi)商品總件數(shù)和總價的計算及顯示

首先我們來看下實際運行效果

2180072-eb25ddf150bd3966.png
2180072-9dcf22670887d4c7.png

頁面相關(guān)樣式(不做贅述)

*{
    padding:0px;
    margin:0px;
    font-family:Arial, 'Microsoft YaHei', Helvetica, 'Hiragino Sans GB';
}
html{
      font-size:10px;
}
.page{
    background-color:#f8f8f8;
    position: absolute;
    top: 0px;
    left: 0px;
    right: 0px;
    bottom: 60px;
    overflow: auto;
    text-align: left;
    font-size: 2rem;
}
nav{
    position: absolute;
    bottom: 0px;
    left: 0px;
    right: 0px;
    height: 60px;
    display: flex;
    border-top:1px solid #ededed;
    background-color: #fff;
}

nav a:link,nav a:visited{
    text-decoration:none;
    flex: 1;
    text-align: center;
    box-sizing: border-box;
    /*      border-right: 1px solid #ededed;*/
    color: #666;
    padding-top: 5px;
    position: relative;
}
nav a:last-child{
    border-right: none;
}
nav a.active{
    color: #FF4354;
}
nav a i{
    display: block;
    margin: 0 auto;
    width: 25px;
    height: 25px;
}
nav a .tip{
    display: block;
    width: 10px;
    height: 10px;
    position: absolute;
    line-height: 10px;
    left: 50%;
    top: -5px;
    padding: 5px;
    transform: translateX(5px);
    background-color: #FF4354;
    color: #fff;
    border-radius: 50%;
}
nav a.home.active i{
      background: url('images/nav-home-on.png') no-repeat center;
      background-size: contain;
    }
nav a.home i{
      background: url('images/nav-home-off.png') no-repeat center;
      background-size: contain;
    }
nav a.topics.active i{
      background: url('images/nav-circle-on.png') no-repeat center;
      background-size: contain;
    }
nav a.topics i{
      background: url('images/nav-circle-off.png') no-repeat center;
      background-size: contain;
    }
nav a.message.active i{
      background: url('images/nav-message-on.png') no-repeat center;
      background-size: contain;
    }
nav a.message i{
      background: url('images/nav-message-off.png') no-repeat center;
      background-size: contain;
    }
nav a.user.active i{
      background: url('images/nav-mine-on.png') no-repeat center;
      background-size: contain;
    }
nav a.user i{
      background: url('images/nav-mine-off.png') no-repeat center;
      background-size: contain;
    }
.goods-list{
}
.goods-item{
  background-color: #ffffff;
  box-sizing: border-box;
  float: left;
  width: 50%;
  border-right: 1px solid #ededed;
  text-align: left;
}
.goods-item img{
      width: 100%;
}
.goods-item .item-op{
    color: #FF4354;
    padding: 10px 15px;
    border-bottom: 1px solid #ededed;
}
.goods-item  a:link,.goods-item a:visited{
    color: #FF4354;
}
.goods-item .item-con{
    padding: 10px 15px;
    border-bottom: 1px solid #ededed;
    color: #717171;
}
.goods-item .item-con .add{
  display: inline-block;
  padding: 5px 10px;
  background-color:#ff4354;
  color: #ffffff;
  border-radius: 50%;
  margin: 0px;
}
.c_333{
  color: #333;
}
.c_ccc{
  color: #ccc;
}
.c_ff4354{
  color:#ff4354;
}
.f-r{
  float: right;
}
.goods-item  .item-btn{
    display: inline-block;
    padding: 0px 10px;
    margin-left: 10px;
}
.shopcar-items{
  text-align: left;
}
.shopcar-item{
  font-size: 1.5rem;
  background-color: #ffffff;
  position: relative;
  padding: 10px 10px 10px 70px;
  border-bottom: 1px solid #ededed;
}
.shopcar-item img{
      width: 100%;
}
.shopcar-item .item-img{
  position: absolute;
  left: 10px;
  top:10px;
  width:50px;
  height:50px;
  border-radius: 5px;
}
.shopcar-item .item-con .mul-btn,.shopcar-item .item-con .add-btn{
  display: inline-block;
  padding: 5px 10px;
  background-color:#ff4354;
  color: #ffffff;
  border-radius: 5px;
  margin: 0px 5px;
}
.total-info{
  padding: 10px 15px;
  text-align: left;
}

.nothing{
  padding: 40px;
}
.nothing div{
  background-color: #ff4354;
  color: #ffffff;
  border-radius: 10px;
  margin: 0 auto;
  padding: 10px;
  text-align: center;
}

頁面相關(guān)標(biāo)簽結(jié)構(gòu)

<body ng-app=""  ng-controller="myTabCtrl">
 <div class="pages">
     <div class="page" ng-show="focusIndex==0">
       <!--商品列表-->
        <div class="goods-list">
           <div class="goods-item" ng-repeat="item in goods">
              <div class="item-op">
                ![]({{item.url}})
              </div>
              <div class="item-con">
                <p>{{item.name}}</p>
                <p>
                <span class="c_ff4354">¥{{item.price}}</span>
                <span class="f-r c_ccc add" ng-click="addToShopcar(item)">+</span>
              </p>
              </div>
           </div>
        </div>
       <!--商品列表/-->
     </div>
     <div class="page" ng-show="focusIndex==1">游記內(nèi)容</div>
     <div class="page" ng-show="focusIndex==2">
       <!--購物車商品列表-->
       <div class="total-info">總商品數(shù):{{shopCarItems.length}} 件;總價:¥<span class="c_ff4354">{{totalMoney}}</span>元</div>
        <div class="shopcar-items">
           <div class="shopcar-item" ng-repeat="item in shopCarItems">
              <div class="item-img">
                ![]({{item.url}})
              </div>
              <div class="item-con">
                <p>{{item.name}}</p>
                <p>
                  <span class="c_ff4354">¥{{item.price}}</span>
                  <span class="f-r c_ccc">
                  <span class="mul-btn" ng-click="mulGoodAmount($index)">-</span>
                  {{item.amount}}
                  <span class="add-btn"  ng-click="addToShopcar(item)">+</span>
                </span>
              </p>
              </div>
           </div>
        </div>
       <!--購物車商品列表/-->

       <div class="nothing" ng-if="shopCarItems.length==0">
           <div ng-click="focus(0)">去購物</div>
       </div>

     </div>
     <div class="page" ng-show="focusIndex==3">個人中心內(nèi)容</div>
 </div>
  <nav>
   <a class="home" ng-class="{'active':focusIndex==0}" href="javascript:;" ng-click="focus(0)"><i></i>優(yōu)選圈</a>
   <a class="topics" ng-class="{'active':focusIndex==1}" href="javascript:;" ng-click="focus(1)"><i></i>游記</a>
   <a class="message" ng-class="{'active':focusIndex==2}" href="javascript:;" ng-click="focus(2)"><i></i>購物車<span class="tip" ng-if="shopCarItems.length">{{shopCarItems.length}}</span></a>
   <a class="user" ng-class="{'active':focusIndex==3}" href="javascript:;" ng-click="focus(3)"><i></i>個人中心</a>
  </nav>
</body>

頁面的angular代碼、實現(xiàn)邏輯及解釋

<script>
//顯示操作成功
      function showAltMsg(msg){
        var toast=document.getElementById('toast');
        if(toast){
          toast.innerHTML=msg;
          toast.style.display="block";
        }else{
          var msgDom=document.createElement('div');
          msgDom.id='toast';
          msgDom.innerHTML=msg;
          var body=document.getElementsByTagName('body')[0]
          body.appendChild(msgDom);
        }

        setTimeout(function(){
          var toast=document.getElementById('toast');
          toast.style.display="none";
        },2000);
      }


    function myTabCtrl($scope) {
        //設(shè)置當(dāng)前聚焦的索引
        $scope.focusIndex = 0;
        //設(shè)置聚焦方法
        $scope.focus = function(index) {
            $scope.focusIndex = index;
        }
        //默認(rèn)商品列表
        $scope.goods = [
            {
                price: 28,
                id: 1,
                name: "滿減【三只松鼠_琥珀核桃仁165g】堅果特產(chǎn)休閑零食紙",
                url: 'http://img10.360buyimg.com/n1/jfs/t2821/18/1316712878/430655/c65ffb7/573c06a0N956686b0.jpg',
                left: '4/50'
            }, {
                price: 28,
                id: 2,
                name: "旺旺旺仔牛奶2940ml原味245 ml× 8罐+蘋果味245ml×4罐",
                url: 'http://img14.360buyimg.com/n1/jfs/t2623/71/2055615738/228070/6f87f326/57553c7aNa8b4b9c3.jpg',
                left: '4/50'
            }, {
                price: 28,
                id: 3,
                name: "旺旺旺仔牛奶2940ml原味245 ml× 8罐+蘋果味245ml×4罐",
                url: 'http://img14.360buyimg.com/n1/jfs/t2623/71/2055615738/228070/6f87f326/57553c7aNa8b4b9c3.jpg',
                left: '4/50'
            }, {
                price: 28,
                id: 4,
                name: "滿減【三只松鼠_琥珀核桃仁165g】堅果特產(chǎn)休閑零食紙",
                url: 'http://img10.360buyimg.com/n1/jfs/t2821/18/1316712878/430655/c65ffb7/573c06a0N956686b0.jpg',
                left: '4/50'
            }, {
                price: 28,
                id: 5,
                name: "滿減【三只松鼠_琥珀核桃仁165g】堅果特產(chǎn)休閑零食紙",
                url: 'http://img10.360buyimg.com/n1/jfs/t2821/18/1316712878/430655/c65ffb7/573c06a0N956686b0.jpg',
                left: '4/50'
            }, {
                price: 28,
                id: 6,
                name: "旺旺旺仔牛奶2940ml原味245 ml× 8罐+蘋果味245ml×4罐",
                url: 'http://img14.360buyimg.com/n1/jfs/t2623/71/2055615738/228070/6f87f326/57553c7aNa8b4b9c3.jpg',
                left: '4/50'
            }, {
                price: 28,
                id: 7,
                name: "滿減【三只松鼠_琥珀核桃仁165g】堅果特產(chǎn)休閑零食紙",
                url: 'http://img10.360buyimg.com/n1/jfs/t2821/18/1316712878/430655/c65ffb7/573c06a0N956686b0.jpg',
                left: '4/50'
            }, {
                price: 28,
                id: 8,
                name: "旺旺旺仔牛奶2940ml原味245 ml× 8罐+蘋果味245ml×4罐",
                url: 'http://img14.360buyimg.com/n1/jfs/t2623/71/2055615738/228070/6f87f326/57553c7aNa8b4b9c3.jpg',
                left: '4/50'
            }, {
                price: 28,
                id: 9,
                name: "滿減【三只松鼠_琥珀核桃仁165g】堅果特產(chǎn)休閑零食紙",
                url: 'http://img10.360buyimg.com/n1/jfs/t2821/18/1316712878/430655/c65ffb7/573c06a0N956686b0.jpg',
                left: '4/50'
            }
        ]
        //購物車商品;列表
        $scope.shopCarItems = [
            {
                price: 28,
                id: 1,
                name: "滿減【三只松鼠_琥珀核桃仁165g】堅果特產(chǎn)休閑零食紙皮核桃",
                url: 'http://img10.360buyimg.com/n1/jfs/t2821/18/1316712878/430655/c65ffb7/573c06a0N956686b0.jpg',
                amount: 2
            }, {
                price: 28,
                id: 2,
                name: "旺旺旺仔牛奶2940ml原味245 ml× 8罐+蘋果味245ml×4罐",
                url: 'http://img14.360buyimg.com/n1/jfs/t2623/71/2055615738/228070/6f87f326/57553c7aNa8b4b9c3.jpg',
                amount: 1
            }
        ]
        //添加商品到購物車
        $scope.addToShopcar = function(item) {
            var realIndex = $scope.getRealIndex(item.id);

            /**
              得到的真實索引>=0則代表商品已經(jīng)在購物車內(nèi)
              若已經(jīng)在則增加商品數(shù)量
              若不在則增加一個商品條目
          **/
            if (realIndex >= 0) {
                $scope.shopCarItems[realIndex].amount += 1;
            } else {
                item.amount = 1;
                $scope.shopCarItems.push(item);
            }
            //增加商品數(shù)量或條目則重新計算購物車商品總金額
            $scope.getTotalMoney();
            showAltMsg("添加到購物車成功");
        }
        //獲得商品在購物車中的真實索引
        $scope.getRealIndex = function(id) {
            var realIndex = -1;
            for (var i = 0; i < $scope.shopCarItems.length; i++) {
                if ($scope.shopCarItems[i].id == id) {
                    realIndex = i;
                    break;
                }
            }
            return realIndex;
        }
        //計算購物車總額
        $scope.getTotalMoney = function() {
            var sum = 0;
            for (var i = 0; i < $scope.shopCarItems.length; i++) {
                sum += $scope.shopCarItems[i].price * $scope.shopCarItems[i].amount
            }
            $scope.totalMoney = sum;
        };

        //初次加載計算一下總金額
        $scope.getTotalMoney();

        //減少購物車商品數(shù)量
        $scope.mulGoodAmount = function(index) {
            $scope.shopCarItems[index].amount -= 1;
            //商品數(shù)量減少到0時直接刪除商品
            if ($scope.shopCarItems[index].amount == 0) {
                $scope.shopCarItems.splice(index, 1);
            }
            //減少商品數(shù)量之后重新計算購物車商品總金額
            $scope.getTotalMoney();
        }
    }
</script>

主要指令

  • ng-app
  • ng-controller
  • ng-repeat
  • ng-if
  • ng-src 這是一個未講過的指令,這個指令的功能是給img標(biāo)簽綁定 src屬性
//數(shù)據(jù)
{
price: 28,
id: 2,
name: "旺旺旺仔牛奶2940ml原味245 ml× 8罐+蘋果味245ml×4罐",
url: 'http://img14.360buyimg.com/n1/jfs/t2623/71/2055615738/228070/6f87f326/57553c7aNa8b4b9c3.jpg',
amount: 1
}
![]({{item.url}})
  • ng-click
  • ng-show
  • ng-class
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,763評論 6 539
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,238評論 3 428
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,823評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,604評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 72,339評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,713評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,712評論 3 445
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,893評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,448評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,201評論 3 357
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,397評論 1 372
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,944評論 5 363
  • 茶點故事閱讀 44,631評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,033評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,321評論 1 293
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,128評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 48,347評論 2 377

推薦閱讀更多精彩內(nèi)容