每次点击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>