0

This is my small basic angular js code :-

var app=angular.module("myApp",[]);
app.controller("MyController",['$scope',function($scope){
$scope.name="Asim";
$scope.f1=function(){   
console.log("i am clicked");
$scope.vr="lol";
}
console.log("angular working");
}]);

And this is my index.html code :-

<html ng-app="myApp">
<head>
<script type="text/javascript" src="/angular.min.js"></script>
<script src="/app.js"></script>

<title> Index </title>
</head>  
<body ng-controller="MyController">    
{{vr}}
{{name}}

<button ng-click="f1()">Click me</button>

 </body>
</html>

When I click the button it shows in console "I am clicked" and "angular is working" does print but the {{name}} and {{vr}} doesn't print. Any possible reason??

Plus i am using nodejs and angular code is in "public" folder i.e static folder and my index.html resides in "views" formed when a project with express-generator is created. I got a hint, maybe "name" and "vr" are loaded after the index.html page is rendered, so they don't print and everything else works, ANYWAY TO FIX SUCH THING?

10
  • Any error in console? Commented Jul 22, 2017 at 19:03
  • No error there. Commented Jul 22, 2017 at 19:04
  • My angular code resides in "public" folder. And the index.html is in "views" folder. Folder pattern is according to express-generator scene. Commented Jul 22, 2017 at 19:05
  • what does it print in places for {{vr}} and even {{name}} should always print, does not depend on click Commented Jul 22, 2017 at 19:06
  • It prints nothing. There is only a button "Click Me" on the screen. I tried Opera and Chrome same case. Commented Jul 22, 2017 at 19:09

2 Answers 2

1

You can use ng-bind. Only your name will render because the other variable is getting initialised inside the ng-click. You can simply use one way binding.

<span ng-bind="name"></span>
Sign up to request clarification or add additional context in comments.

1 Comment

There might be any css problem on your view. Maybe that is why the data is not appearing in the view port of the screen when you are using vanilla {{name}} inside body. Using span will make sure that your data is tied to a valid html
0

You code seems to be working, I think, am I missing something?

<html ng-app="myApp">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
<script>

var app=angular.module("myApp",[]);
app.controller("MyController",['$scope',function($scope){
    $scope.name="Asim";
    $scope.f1=function(){
        console.log("i am clicked");
        $scope.vr="lol";
    }
    console.log("angular working");
}]);
</script>

<title> Index </title>
</head>
<body ng-controller="MyController">
    {{vr}}
    {{name}}
    <button ng-click="f1()">Click me</button>
 </body>
</html>

3 Comments

No, see my app.js and index.html are in different files.
Then I guess you app.js file is not loaded in html tag maybe try <script src="./public/app.js"></script> or <script src="./app.js"></script>
I think script is loading thats why I see "angular working" in console.

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.