admin管理员组

文章数量:1391993

is it possible to register Javascript in a controller action in Yii2?

More or less I think:

class MyController extends yii\web\Controller {
    public function actionView(){
        //Is it possible to register Javascript here?

        return $this->render('view');
    }
} 

is it possible to register Javascript in a controller action in Yii2?

More or less I think:

class MyController extends yii\web\Controller {
    public function actionView(){
        //Is it possible to register Javascript here?

        return $this->render('view');
    }
} 
Share Improve this question asked Dec 1, 2017 at 15:53 Alberto FavaroAlberto Favaro 1,8304 gold badges24 silver badges47 bronze badges 1
  • In yii 1.x you could register the script through clientScript ponent – Alberto Favaro Commented Dec 1, 2017 at 16:39
Add a ment  | 

1 Answer 1

Reset to default 6

Your controller gives you access to its view, which in turn allows you to render JS.

$this->getView()->registerJs("yourJsGoesHere");

http://www.yiiframework./doc-2.0/yii-base-controller.html#getView()-detail

http://www.yiiframework./doc-2.0/yii-web-view.html#registerJs()-detail

本文标签: javascriptYii2 register JS in controllerStack Overflow