javascript - Angular variable not being displayed in HTML - Stack Overflow

I am trying to refresh my knowledge of Angular, and I can't seem to understand a few concepts, the

I am trying to refresh my knowledge of Angular, and I can't seem to understand a few concepts, the "controller as" pattern doesn't seem to be working, for example, even though it seems to be much more simple than $scope.

I can't get a simple variable to show up in my HTML.

Here are two pieces of code in question:

app.js

angular
    .module('routerApp', [''])
    .controller('mainController', function () {
        'use strict';
        var vm = this;
        vm.bigMessage = 'A smooth sea never made a skilled sailor';
    })

index.html (stripped down heavily)

<!DOCTYPE html>
<html>
    <head>
        <script src="//ajax.googleapis/ajax/libs/angularjs/1.3.16/angular.min.js"></script>
        <script src="js/app.js"></script>
    </head>
    <body class="container" ng-app="routerApp" ng-controller="mainController as main">
        <h1>{{ main.bigMesssage }}</h1>
    </body>
</html>

The interesting thing is that the browser doesn't even render "{{ main.bigMessage }}", instead it shows nothing. Yet, in the source code there is the {{ ... }} part.

I am trying to refresh my knowledge of Angular, and I can't seem to understand a few concepts, the "controller as" pattern doesn't seem to be working, for example, even though it seems to be much more simple than $scope.

I can't get a simple variable to show up in my HTML.

Here are two pieces of code in question:

app.js

angular
    .module('routerApp', [''])
    .controller('mainController', function () {
        'use strict';
        var vm = this;
        vm.bigMessage = 'A smooth sea never made a skilled sailor';
    })

index.html (stripped down heavily)

<!DOCTYPE html>
<html>
    <head>
        <script src="//ajax.googleapis./ajax/libs/angularjs/1.3.16/angular.min.js"></script>
        <script src="js/app.js"></script>
    </head>
    <body class="container" ng-app="routerApp" ng-controller="mainController as main">
        <h1>{{ main.bigMesssage }}</h1>
    </body>
</html>

The interesting thing is that the browser doesn't even render "{{ main.bigMessage }}", instead it shows nothing. Yet, in the source code there is the {{ ... }} part.

Share Improve this question edited Jun 21, 2015 at 9:22 CodingIntrigue 78.7k32 gold badges176 silver badges177 bronze badges asked Jun 21, 2015 at 8:56 zcsereizcserei 6657 silver badges33 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You had added '' in dependency array bracket which is wrong Angular is going to search for dependency with name '' & obiviously there would not be any dependancy find with that name & that why angular is throwing $injector error in console. , as you don't have any dependency, it should be []

Code

angular
.module('routerApp', []) //<--change here
.controller('mainController', function () {
    'use strict';
    var vm = this;
    vm.bigMessage = 'A smooth sea never made a skilled sailor';
})

Also you have had typo in {{main.bigMesssage}} here bigMesssage should be bigMessage then it would bee

{{main.bigMesssage}}

Demo Plunkr

you have written everything is right except your bigmessage variable spell

 <body class="container" ng-app="routerApp" ng-controller="mainController as main">
        <h1>{{ main.bigMessage }}</h1>
    </body>

controller

angular
    .module('routerApp', [])
    .controller('mainController', function () {
        var vm = this;
        this.bigMessage = 'A smooth sea never made a skilled sailor';
    })

check this out fiddle: https://jsfiddle/bjwov6f1/1/

It was not finding your variable that why it was not showing anything

for more learning go through this link :https://thinkster.io/egghead/experimental-controller-as-syntax

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745457931a4628573.html

相关推荐

  • javascript - Angular variable not being displayed in HTML - Stack Overflow

    I am trying to refresh my knowledge of Angular, and I can't seem to understand a few concepts, the

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信