<a herf="#" ng-click="expression">引发的问题(初学者)

每次点击a标签都会刷新页面每次都会执行一次新的控制器$watch没能达到想要的效果

html

<div class="col-lg-2">
    <ul>
        <li ng-class="{active: importMode=='clipboard'}"><a href="#" ng-click="importMode='clipboard'"><i class="fa fa-clipboard breath-right"></i>Paste</a></li>
        <li  ng-class="{active: importMode=='file'}"><a href="#" ng-click="importMode='file'"><i class="fa fa-file-text breath-right"></i>Upload a file</a></li>
        <li  ng-class="{active: importMode=='url'}"><a href="#" ng-click="importMode='url'"><i class="fa fa-cloud breath-right"></i>From URL</a></li>
        <li  ng-class="{active: importMode=='sample'}"><a href="#" ng-click="importMode='sample'"><i class="fa fa-gift breath-right"></i>Try our samples</a></li>
    </ul>
</div>
<div>
    <i>importMode:</i>{{importMode}}
    <br>
   
</div>

控制器

'use strict';
angular.module('myApp',[
    'ngRoute',

])
    .config(['$routeProvider','$locationProvider',function ($routeProvider,$locationProvider) {
        $routeProvider.when('/view1',{
            templateUrl:'view1.html',
            controller:'myCtrl'
        })
            .otherwise({redirectTo:'/view1'});

    }])
    .controller('myCtrl',function ($scope) {
        $scope.importMode='file';
        $scope.$watch('importMode',function () {
            console.log('importMode',$scope.importMode);
            //             // $scope.importMode='xxx';
        });
      
        // $scope.importMode='file';

    })

解决:禁用a标签href=“javascript:void(0)”

<div class="col-lg-2">
    <ul>
        <li ng-class="{active: importMode=='clipboard'}"><a href="javascript:void(0)" ng-click="importMode='clipboard'"><i class="fa fa-clipboard breath-right"></i>Paste</a></li>
        <li  ng-class="{active: importMode=='file'}"><a href="javascript:void(0)" ng-click="importMode='file'"><i class="fa fa-file-text breath-right"></i>Upload a file</a></li>
        <li  ng-class="{active: importMode=='url'}"><a href="javascript:void(0)" ng-click="importMode='url'"><i class="fa fa-cloud breath-right"></i>From URL</a></li>
        <li  ng-class="{active: importMode=='sample'}"><a href="javascript:void(0)" ng-click="importMode='sample'"><i class="fa fa-gift breath-right"></i>Try our samples</a></li>
    </ul>
</div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值