0

I'm fairly new to Angular, and a simple binding doesn't work for me.

It just shows me {{ trip.name }} and {{trip.created}} just as they're written.

My controller:

(function () {
 "use strict";
 angular.module("app-trips")
 .controller("tripsController", tripsController);
 function tripsController() {
 var vm = this;
 vm.trips = [{
 name: "US Trip",
 created: new Date()
 }, {
 name: "World Trip",
 created = new Date()
 }];

A part of my class:

@section Scripts {
 <script src="~/lib/angular/angular.min.js"></script>
 <script src="~/js/app-trips.js"></script>
 <script src="~/js/tripsController.js"></script>
}
<div class="row" ng-app="app-trips">
 <div ng-controller="tripsController as vm" class="col-md-6 col-md-offset-3">
 <table class="table table-responsive table-striped">
 <tr ng-repeat="trip in vm.trips">
 <td>{{ trip.name }}</td>
 <td>{{ trip.created }}</td>
 </tr>
 </table>
 </div>

the view of the controller:

(function () {
 "use strict";
 angular.module("app-trips", ['ngSanitize']);
})();

BTW - supposedly according to the course I'm following, I don't need ['ngSanitize'] yet, but without it it doesn't even show the DIVs.

EDIT: as @jellyraptor noticed , I had a typo with = instead of :

EDIT 2 :

It was the typo + the [ngSanitize] which I really didn't needed. I fixed the typo and passed an empty array and everything works. Thanks all

asked Aug 28, 2017 at 15:38
1
  • 1
    This is a guess. Based on the fact that it doesn't work without ['ngSanitize'], it seems like you never actually create your angular module. The module is created when you supply a second (array) argument to angular.module(moduleName, [array of dependencies]). Without that array, angular treats the expression as a getter only, and not a setter. Commented Aug 28, 2017 at 15:59

3 Answers 3

1

Either Angular isn't loading or it is encountering an error. Bring up the dev tools with F12 and see if there are errors in the console. Also in the console, you can type 'angular' and hit enter and if it reports that angular is undefined then angular is not loading properly.

answered Aug 28, 2017 at 15:44
Sign up to request clarification or add additional context in comments.

9 Comments

The console is empty , and where can I type inside the consol ? :S
I hit 'angular' in the Debugger section and it found this line :` <script src="/lib/angular/angular.min.js"></script>`
Depends on the browser. Generally the bottom line of the console output is where you can type. Look for > or >> on the console window.
Do you have an ngApp directive somewhere on the page? Maybe on the <HTML> or <BODY> tag? If not, then Angular is never really bootstrapping. (Unless you're bootstrapping it in code)
Oh, it return something.. [Object object] {callbacks: object{ ...} , version...
|
1

Because you are using controllerAs syntax and here the variable is vm. so for access scope object use vm instead of tripsController

<tr ng-repeat="trip in vm.trips">
 <td>{{ trip.name }}</td>
 <td>{{ trip.created }}</td>
</tr>

(function() {
 "use strict";
 angular.module("app-trips", []);
})();
 angular.module("app-trips")
 .controller("tripsController", tripsController);
 function tripsController() {
 var vm = this;
 vm.trips = [{
 name: "US Trip",
 created: new Date()
 }, {
 name: "World Trip",
 created: new Date()
 }];
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.2/angular.min.js"></script>
<div ng-app="app-trips" ng-controller="tripsController as vm" class="col-md-6 col-md-offset-3">
 <table class="table table-responsive table-striped">
 <tr ng-repeat="trip in vm.trips">
 <td>{{ trip.name }}</td>
 <td>{{ trip.created }}</td>
 </tr>
 </table>
</div>

answered Aug 28, 2017 at 15:39

2 Comments

Sorry it's a typo, I changed it to tripsController when I tried to fix this issue. That doesn't help me.
None .. if there were one I would have searched for the answer online..
1

You put an equals sign in your trips array's second object's 'created' attribute.

vm.trips = [{
 name: "US Trip",
 created: new Date()
}, {
 name: "World Trip",
 created = new Date()
}];

should be

vm.trips = [{
 name: "US Trip",
 created: new Date()
}, {
 name: "World Trip",
 created: new Date()
}];
answered Aug 28, 2017 at 15:53

1 Comment

Thank you for seeing that!! Now I'm getting en error in console. See edit

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.