2017-06-19 71 views
-2

NG-点击功能将无法正常工作

var myApp = angular.module('myApp', ['ngRoute']); 
 
myApp.config(function($routeProvider) { 
 
    $routeProvider.when('/', { 
 
    template: '', 
 
    controller: 'DefaultController as dc' 
 
    }).when('/rent', { 
 
    templateUrl: 'views/partials/rent.html', 
 
    controller: 'rentController as rc' 
 
    }).when('/buy', { 
 
    templateUrl: 'views/partials/buy.html', 
 
    controller: 'buyController as bc' 
 
    }); 
 

 
}); //end config 
 

 
myApp.controller('DefaultController', DefaultController); 
 
myApp.controller("rentController", rentController); 
 
myApp.controller("buyController", buyController); 
 

 
function DefaultController() { 
 
    console.log('inside of DefaultController'); 
 
    var vm = this; 
 
    vm.checkPost = function() { 
 
    console.log('checkpost clicked'); 
 
    }; 
 

 
} //end controller 
 

 
function rentController(RealStateService) { 
 
    console.log('inside of rent controller'); 
 
    var vm = this; 
 
    vm.rentArray = []; 
 
    vm.info = false; 
 

 

 
    vm.getProperties = function() { 
 
    console.log('port'); 
 
    RealStateService.serverPractice().then(function(res) { 
 

 
     RealStateService.data.forEach(function(data) { 
 
     if (data.rent) { 
 
      vm.rentArray.push(data); 
 
     } 
 
     }); //end for each 
 

 
    }); //end then 
 
    }; //end getProperties 
 

 
    vm.showInfo = function(index) { 
 
    vm.info = true; 
 
    console.log('in get Info'); 
 
    vm.info = vm.rentArray[index]; 
 
    console.log(vm.info); 
 
    }; //end get info 
 
} 
 

 
function buyController(RealStateService) { 
 
    console.log('inside of buy controller'); 
 
    var vm = this; 
 
    vm.rentArray = []; 
 
    vm.info = false; 
 

 

 
    vm.getProperties = function() { 
 
    RealStateService.serverPractice().then(function(res) { 
 

 
     RealStateService.data.forEach(function(data) { 
 
     if (data.cost) { 
 
      vm.rentArray.push(data); 
 
     } 
 
     }); //end for each 
 

 
    }); //end then 
 
    }; //end getProperties 
 

 
    vm.showInfo = function(index) { 
 
    vm.info = true; 
 
    console.log('in get Info'); 
 
    vm.info = vm.rentArray[index]; 
 
    console.log(vm.info); 
 
    }; //end get info 
 

 
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <title> Weekend Challenge #5</title> 
 
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
 
    <link rel="stylesheet" href="styles/style.css"> 
 
    <script src="vendors/angular.min.js" charset="utf-8"></script> 
 
    <script src="vendors/angular-route.min.js" charset="utf-8"></script> 
 
    <script src="scripts/client.js" charset="utf-8"></script> 
 
    <script src="scripts/services/realState.js" charset="utf-8"></script> 
 

 

 
</head> 
 

 
<body ng-app='myApp'> 
 

 

 
    <div class="container-fluid"> 
 

 
    <div class="navBar"> 
 
     <h1>Weekend Challenge #5</h1> 
 
     <button type="button" name="button"><a href="http://localhost:3000/#!/rent">See for rent</a></button> 
 
     <button type="button" name="button"><a href="http://localhost:3000/#!/buy">See for sale</a></button> 
 
     <button type="button" name="button"><a href="http://localhost:3000/#!/">post rent/sale</a></button> 
 

 
    </div> 
 
    <ng-view></ng-view> 
 
    <h1>Post</h1> 
 
    <div class="post"> 
 

 
     <label for="type">Type: </label> 
 
     <select name='type' ng-model='dc.type'> 
 
        <option value="">Rent</option> 
 
        <option value="">Sell</option> 
 
       </select> 
 

 
     <button type="button" name="button" ng-click='dc.checkPost()'>Begin</button> 
 
    </div> 
 
    </div> 
 
</body> 
 

 
</html>

应用程序是不是因此运行在这里,但基本上当我点击开始按钮,我没有得到日志的默认控制器。我不确定问题是什么。我应该看到在控制台'checkpost cliked'

+0

您的按钮标记未链接到您的控制器 – Tik

+0

您的控制器正在按页面加载吗? –

回答

0

我想你忘了给angular-route.js。再次尝试添加以下脚本链接

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>