简体   繁体   中英

AngularJS doesn't work with yii2

I'm trying to get simple Hello World from yii2 layout. AppAsset.php:

...
    public $js = [
       'js/angular.min.js',
    ];
...

layout/main.php:

...
<html lang="<?= Yii::$app->language ?>" ng-app="app">
...
<body ng-contoller="ctrl">
...
    <div class="container">
        {{hello}}
...
<?php $this->endBody() ?>
<?=Html::jsFile('@web/js/app.js')?>
</body>
...

/js/app.js

angular.module('app', [])
       .controller('ctrl', function($scope){
            $scope.hello = 'Hello World!';
            console.log('inside ctrl');
    });

console.log('outside ctrl');

'outside ctrl' works fine. but block 'inside ctrl' doesn't. Page not shows anything about {{hello}}. Where I missed?

Why are you loading the app js outside the AppAsset? Should be there too in order to work.

If you dont want to do that at lest load de app js above the endbody function.

You need to just include your app.js file in AppAsset.php like as

public $js = [
    'js/angular.min.js',
    'js/app.js',
];

its working for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM