PenVirus PenVirus - 2 months ago 7
AngularJS Question

UI-Router controller isn't working

I'm new to using UI-Router, and I'm struggling to get this controller working. The view loads, but the function doesn't trigger on ng-click. JSFiddle

var app = angular.module('app', ['ui.router']);

app.controller('dashboardCtrl', [
'$scope',
function($scope){
$scope.testFunction = function(){
console.log('test');
}
}
]);

app.config(function ($stateProvider, $urlRouterProvider){
$stateProvider.state("dashboard", {
url: "#",
templateUrl: "dashboard.html",
controller: "dashboardCtrl"
});
});


View

<body ng-app="app">
<nav>
<a ui-sref="dashboard">Dashboard</a>
</nav>
<ui-view></ui-view>
</body>

<script type = "text/ng-template" id = "dashboard.html">
<button ng-click="testfunction()">Click</button>
</script>

Answer

The testfunction in your template not working because there no function in controller called testfunction. In controller you specified it as testFunction. Note the capital letter 'F' in it. It will work if you change it. but, please not that url: "#" is not valid URL

Comments