报错代码如下:
1 2 3 4 5 6 7 8 9 | < div ng-controller="HelloAngular"> < p >{{greeting.text}},angular</ p > </ div > function HelloAngular($scope){ $scope.greeting = { text:'hello' } } |
在用angular1.3版本以前的时候,上面的代码运行是没有问题的。但是我更新版本后报错
1 2 3 4 5 6 7 8 9 10 11 | Error: [ng:areq] http://errors.angularjs.org/1.5.8/ng/areq?p0=*&p1=not%20a%20function%2C%20got%20undefined at Error (native) at http://localhost:63342/untitled1/public/javascripts/lib/angular.min.js:6:412 at sb (http://localhost:63342/untitled1/public/javascripts/lib/angular.min.js:23:18) at Pa (http://localhost:63342/untitled1/public/javascripts/lib/angular.min.js:23:105) at http://localhost:63342/untitled1/public/javascripts/lib/angular.min.js:89:310 at ag (http://localhost:63342/untitled1/public/javascripts/lib/angular.min.js:72:419) at p (http://localhost:63342/untitled1/public/javascripts/lib/angular.min.js:64:262) at g (http://localhost:63342/untitled1/public/javascripts/lib/angular.min.js:58:481) at g (http://localhost:63342/untitled1/public/javascripts/lib/angular.min.js:58:498) at g (http://localhost:63342/untitled1/public/javascripts/lib/angular.min.js:58:498) |
因为不允许定义全局函数来污染全局对象,做好放在module.controller()这样一个函数中来操作model数据
修改后的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | mvc.js var app = angular.module('userApp',[]); app.controller('HelloAngular',function($scope){ $scope.greeting = { text:'hello' } }) <! doctype html> < html ng-app="userApp"> < head > < meta charset="utf-8"> </ head > < body > < div > < div ng-controller="HelloAngular"> < p >{{greeting.text}},angular</ p > </ div > </ div > </ body > < script src="../public/javascripts/lib/angular.min.js"></ script > < script src="../public/javascripts/test/mvc.js"></ script > </ html > |
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步