Rafael - Developer Rafael - Developer - 2 months ago 6
AngularJS Question

Controller does not work in route

I would like to add link with controller to route but controller does not work.
enter image description here
I get this:


{{title}}

{{shortTitle}}

{{text}}


If I will put controller only to html also does not work

enter image description here

but when I added script to main controller in Default.html controller working correctenter image description here
Where I made mistake?

@SSH this not work

$stateProvider
.state('home', {
url: '/',
template: '<div data-ng-include="&#39;Scripts/js_angular_project/website/home/homePage.html&#39;"></div>',
controller: 'homeCtrl'
})


homePage.html

<div>
<h2>{{title}}</h2>
<h3>{{shortTitle}}</h3>
<p>{{text}}</p>
</div>
<script>
app.controller('homeCtrl', function ($scope) {
$scope.title = "Volvo";
$scope.shortTitle = "Volvo";
$scope.text = "example";
});
</script>


@SSH this also does not work

$stateProvider
.state('home', {
url: '/',
templateURL: '/Scripts/js_angular_project/website/home/homePage.html',
controller: 'homeCtrl'
})


homePage.HTML - I remove ng-controller

<div>
<h2>{{title}}</h2>
<h3>{{shortTitle}}</h3>
<p>{{text}}</p>
</div>
<script>
app.controller('homeCtrl', function ($scope) {
$scope.title = "Volvo";
$scope.shortTitle = "Volvo";
$scope.text = "example";
});
</script>


@SSH this not work when I put my code

enter image description here

.html

enter image description here

SSH SSH
Answer

var routerApp = angular.module('routerApp', ['ui.router']);
routerApp.config(function($stateProvider, $urlRouterProvider) {
    
    $urlRouterProvider.otherwise('/home');
    
    $stateProvider
        .state('home', {
            url: '/home',
            template: '<div><h2>{{title}}</h2><h3>{{shortTitle}}</h3><p>{{text}}</p></div>',
            controller:'homeCtrl'
        })
        .state('about', {
            url: '/about',
             template: '<div><h2>{{title}}</h2><h3>{{shortTitle}}</h3><p>{{text}}</p></div>',
             controller:'aboutCtrl'
            })
});

routerApp.controller('homeCtrl', function ($scope) {
        $scope.title = "Volvo";
        $scope.shortTitle = "Volvo";
        $scope.text = "example";
    });
    
    routerApp.controller('aboutCtrl', function ($scope) {
        $scope.title = "Volvo2";
        $scope.shortTitle = "Volvo2";
        $scope.text = "example2";
    });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.10/angular-ui-router.min.js"></script>

<div ng-app="routerApp">
<nav class="navbar navbar-inverse" role="navigation">
    <ul class="nav navbar-nav">
        <li><a ui-sref="home">Home</a></li>
        <li><a ui-sref="about">About</a></li>
    </ul>
</nav>

<div class="container">
    <div ui-view></div>
</div>
</div>

you should put controller name in router and also remove ng-controller = "homeCtrl" from template.

   state('home' ,{
      url : '/',
      templateUrl:'/yourTempalteAddress',
      controller:'homeCtrl' 
    })

and define your app in controller.

    var app = angular.module("yourApp",[]);
    app.controller('homeCtrl', function ($scope) {
     $scope.title = "Volvo";
     $scope.shortTitle = "Volvo";
     $scope.text = "example";
     });
Comments