0

I am in the process of upgrading a large AngularJS 1.4 app to 1.5 using components instead of raw controllers.

I have a basic component looking like this:

 'use strict';

 angular.module('myModule').component('userComponent', {
 controllerAs: 'vm',
 bindings: {
    user: '<'
 },
 controller:
    function userComponent($http) {
      let vm = this;
      vm.user = "World";
 });

When I try to display the value of vm.user in the template, it does not show and there is no error in the console:

 <user-component>
    <h1>Hello {{vm.user}}</h1>
 </user-component>

Can you tell me what is wrong? This should be fairly simple and I am not sure why it is not doing the binding as expected.

2 Answers 2

1

You can't fill a component template like that. You have to define a template linked to your component.

 'use strict';

 angular.module('myModule').component('userComponent', {
 controllerAs: 'vm',
 template: '<h1>Hello {{vm.user}}</h1>',
 bindings: {
    user: '<'
 },
 controller:
    function UserComponent($http) {
      let vm = this;
      vm.user = "World";
 });
Sign up to request clarification or add additional context in comments.

3 Comments

shouldn't component names start with a lower case letter: 'userComponent'?
@AlekseySolovey Yes they should, I forgot to mention that in my answer, plus there is a missing curly brace after the controller definition
@AlekseySolovey Good spot but this is not the actual issue. I edited my post to remove the upperCase component name. Thank you.
1

I don't see a templateUrl or template definition in your component, which are required.

You can add this template:"<span>Name: {{vm.user}}</span>" or link a html page. You can read more in the documentation here

(function(angular) {
  'use strict';
  angular.module('test', []).controller('MainCtrl', function MainCtrl() {});


  angular.module('test').component('userComponent', {
    controllerAs: 'vm',
    bindings: {
      user: '<'
    },
    controller: function UserComponent($http) {
      let vm = this;
      vm.user = "World";
    },
    template: "<span>Name: {{vm.user}}</span>"
  });
})(window.angular);
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.10/angular.min.js"></script>

<body ng-app="test">
  <!-- components match only elements -->
  <div ng-controller="MainCtrl as ctrl">
    <user-component></user-component>
  </div>
</body>

5 Comments

I do have templateUrl in my route.js file like this: .state('operatorHome', { url: '/user', component: 'UserComponent', views: { '': { templateUrl: 'templates/user/user.html' } }, })
I would like to add the templateUrl inside my component but this will create a 404 not found Error.
As George shown you, you can use template and write in directly your component your HTML template
@Zooly the template will be quite large, I'm not very comfortable in doing this.
@NizarB. Looking at ui-routers documentation (which is what I assume you're using) the template/templateUrl has to be defined on the component, although you've mentioned that moving it causes a 404, can you not just update it to have the right relative url? Not knowing your project folder structure I can't answer what you'd need to change it to

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.