(angularjs)注入控制器时收到"ReferenceError:未定义$ scope" [英] (angularjs) getting 'ReferenceError: $scope is not defined' when injecting controller

查看:69
本文介绍了(angularjs)注入控制器时收到"ReferenceError:未定义$ scope"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习AngularJS.我有一个HTML页面,我试图在其中插入模块但出现错误

I have am trying to learn AngularJS. I have a HTML page where I am trying to inject module but getting an error

主要js文件:

var classificationModule = angular.module('mainapp',[]);

classificationModule.controller('firstcontroll',function(){
    $scope.text="Goodday"
});

HTML 页面:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="layout" content="main"/>
    <title>Classification Toolkit for Grails</title>          

    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js"></script>
    <asset:javascript src="main.js"/>        

</head>
<body >   
    <div ng-app='mainapp' ng-controller='firstcontroll'>
        Hello, {{text}}
    </div>   

</body>
</html>

出现以下错误:

ReferenceError: $scope is not defined

我不知道我在做什么错.

I dont know what I am doing wrong.

推荐答案

尝试在函数中传递 $ scope .

var classificationModule = angular.module('mainapp', []);

classificationModule.controller('firstcontroll', function($scope) {
  $scope.text = "Goodday"
});

<!DOCTYPE html>
<html>

<head lang="en">
  <meta charset="UTF-8">
  <meta name="layout" content="main" />
  <title>Classification Toolkit for Grails</title>

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.21/angular.js"></script>
  <asset:javascript src="main.js" />

</head>

<body>
  <div ng-app='mainapp' ng-controller='firstcontroll'>
    Hello, {{text}}
  </div>

</body>

</html>

这篇关于(angularjs)注入控制器时收到"ReferenceError:未定义$ scope"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆