简体   繁体   English

AngularJS不适用于yii2

[英]AngularJS doesn't work with yii2

I'm trying to get simple Hello World from yii2 layout. 我正在尝试从yii2布局中获取简单的Hello World。 AppAsset.php: AppAsset.php:

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

layout/main.php: 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 /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. '外部ctrl'工作正常。 but block 'inside ctrl' doesn't. 但不会阻止“ inside ctrl”。 Page not shows anything about {{hello}}. 页面未显示有关{{hello}}的任何信息。 Where I missed? 我在哪里错过?

Why are you loading the app js outside the AppAsset? 为什么要在AppAsset外部加载应用js? 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. 如果您不想这样做,以免在endbody函数上方加载de js应用程序。

You need to just include your app.js file in AppAsset.php like as 你只是需要包括你的app.js文件AppAsset.php等作为

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

its working for me. 它为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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