0

I have the following code:

Trips.cshtml:

@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 >{{"Two plus two : " + (2+2)}}</div>
  <div ng-controller="tripsController as vm" class="col-md-6 col-md-offset-3">
      {{ vm.name }}
   </div>
  </div>

app-trips.js:

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

tripsController.js:

(function () {
    "use strict";
    angular.module("app-trips")
        .controller("tripsController", tripsController);
    function tripsController() {
        var vm = this;
        vm.name = "jaka";
    }
})();

In my .cshtml I get "2+2" = 4 shown in angular, but I don't get my vm.name called. Does anyone know what might be the problem?

2 Answers 2

1

You have not closed the div,

<div class="row" ng-app="app-trips">
<div >{{"Two plus two : " + (2+2)}}</div>
  <div ng-controller="tripsController as vm" class="col-md-6 col-md-offset-3">
      {{ vm.name }}
  </div>
</div>

DEMO

 angular.module("app-trips", []).controller("tripsController", tripsController);
 function tripsController() {
        var vm = this;
        vm.name = "jaka";
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="row" ng-app="app-trips">
<div >{{"Two plus two : " + (2+2)}}</div>
<div ng-controller="tripsController as vm" class="col-md-6 col-md-offset-3">
      {{ vm.name }}
</div>

Sign up to request clarification or add additional context in comments.

6 Comments

My bad, it is closed, I just didn't add that here.
Wow... But for real, it is not working at my project. "vm.name" (not jaka) shows for 0.5 seconds and than disappears .
Then it must be something wrong with your project setup
do you have any suggestions what might cause this problems in my setup?
check the scripts loading order and also console error
|
1

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title></title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.6.5/angular.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.0/angular-route.min.js"></script>
    <!-- <link href="Styles.css" rel="stylesheet" /> -->
   <script>
 (function () {
   "use strict";
   angular.module("app-trips", []);
  })();
  (function () {
    "use strict";
    angular.module("app-trips")
        .controller("tripsController", tripsController);
    function tripsController() {
        var vm = this;
        vm.name = "jaka";
    }
})();
   </script>
</head>
<body >
   <div class="row" ng-app="app-trips">
<div >{{"Two plus two : " + (2+2)}}</div>
  <div ng-controller="tripsController as vm" class="col-md-6 col-md-offset-3">
      {{ vm.name }}
  </div>
</body>
</html>

3 Comments

Wow this works... But for real, it is not working at my project. "vm.name" shows for 0.5 seconds and than disappears.
try to place internal js as i did. and check..does it working or not. because i checked your code . there is nothing wrong about it .. or remove function and declare global variable for debugging
I think I found the solution. I had Nugget package Angular installed as well as Angular in bower.json file. I uninstalled Angular, deleted package in /wwwroot/lib folder and added it to the bower again. Now it works.

Your Answer

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.