javascript - Angular controlller控制域和原生js的關(guān)系
問(wèn)題描述
寫了一段js代碼,發(fā)現(xiàn)在controller里寫的原生js都不管用,報(bào)錯(cuò)顯示無(wú)法找到該函數(shù),代碼如下所示:
app.controller(’orderFormCtrl’, function ($scope, $http) { // 訂單備注 $scope.food_remarks = []; // 訂單詳情數(shù)組 $scope.forms_detail = []; $scope.food_detail = []; // 查詢訂單詳情 $scope.search_detail = function(index){ $scope.forms_detail = $scope.food_forms[index];$scope.food_remarks = $scope.food_forms[index].remarks;console.log($scope.forms_detail);// var form_num = $scope.forms_detail.order_number;$scope.food_detail = $scope.food_forms[index].food_detail;// console.log($scope.food_detail);for (var i = 0; i < $scope.food_detail.length; i++) { for (var j = 0; j < $scope.food_menu.length; j++) {if ($scope.food_detail[i].id == $scope.food_menu[j].id) { $scope.food_detail[i].name = $scope.food_menu[j].name; $scope.food_detail[i].price = $scope.food_menu[j].price; $scope.food_detail[i].img = $scope.food_menu[j].img;} }} }; //計(jì)算總價(jià) $scope.allSum=function(){ var allPrice = 0; for(var i= 0;i<$scope.food_detail.length;i++){ allPrice+=$scope.food_detail[i].price*$scope.food_detail[i].num; // console.log(allPrice);} return allPrice; };var LODOP; //聲明為全局變量 function printPreview(){ //創(chuàng)建小票打印頁(yè) CreatePrintPage(); //打印預(yù)覽 LODOP.PREVIEW(); } /** * 樣例函數(shù),服務(wù)器確認(rèn)訂單后執(zhí)行 */ function printOrder() {//創(chuàng)建小票打印頁(yè) CreatePrintPage(); //開(kāi)始打印 LODOP.PRINT(); } function CreatePrintPage(json) { //json 創(chuàng)建模擬服務(wù)器響應(yīng)的訂單信息對(duì)象 var json = {'title':'XXXXX訂單信息', 'name':'張三', 'phone': '138123456789', 'orderTime': '2012-10-11 15:30:15', 'orderNo': '20122157481315', 'shop':'XX連鎖', 'total':25.10,'totalCount':6, 'goodsList':[ {'name':'菜心(無(wú)公害食品)', 'price':5.00, 'count':2, 'total':10.08}, {'name':'菜心(無(wú)公害食品)', 'price':5.00, 'count':2, 'total':10.02}, {'name':'旺菜', 'price':4.50, 'count':1, 'total':4.50}, {'name':'黃心番薯(有機(jī)食品)', 'price':4.50, 'count':1, 'total':4.50} ] } var hPos=10,//小票上邊距 pageWidth=580,//小票寬度 rowHeight=15,//小票行距 //獲取控件對(duì)象 LODOP=getLodop(document.getElementById(’LODOP_OB’),document.getElementById(’LODOP_EM’)); //初始化 LODOP.PRINT_INIT('打印控件功能演示_Lodop功能_名片'); //添加小票標(biāo)題文本 LODOP.ADD_PRINT_TEXT(hPos,30,pageWidth,rowHeight,json.title); //上邊距往下移 hPos+=rowHeight; LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,'姓名:'); LODOP.ADD_PRINT_TEXT(hPos,30,pageWidth,rowHeight,json.name); //hPos+=rowHeight; //電話不換行 LODOP.ADD_PRINT_TEXT(hPos,70,pageWidth,rowHeight,'電話:'); LODOP.ADD_PRINT_TEXT(hPos,100,pageWidth,rowHeight,json.phone); hPos+=rowHeight; LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,'下單時(shí)間:'); LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.orderTime); hPos+=rowHeight; LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,'訂單編號(hào):'); LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.orderNo); hPos+=rowHeight; LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,'取貨門店:'); LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.shop); hPos+=rowHeight; LODOP.ADD_PRINT_LINE(hPos,2, hPos, pageWidth,2, 1); hPos+=5; LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,'商品名稱'); LODOP.ADD_PRINT_TEXT(hPos,70,pageWidth,rowHeight,'單價(jià)'); LODOP.ADD_PRINT_TEXT(hPos,110,pageWidth,rowHeight,'數(shù)量'); LODOP.ADD_PRINT_TEXT(hPos,140,pageWidth,rowHeight,'小計(jì)'); hPos+=rowHeight; //遍歷json的商品數(shù)組 for(var i=0;i<json.goodsList.length;i++){ if(json.goodsList[i].name.length<4){ LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,json.goodsList[i].name); }else { //商品名字過(guò)長(zhǎng),其他字段需要換行 LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,json.goodsList[i].name); hPos+=rowHeight; } LODOP.ADD_PRINT_TEXT(hPos,70,pageWidth,rowHeight,json.goodsList[i].price); LODOP.ADD_PRINT_TEXT(hPos,115,pageWidth,rowHeight,json.goodsList[i].count); LODOP.ADD_PRINT_TEXT(hPos,140,pageWidth,rowHeight,json.goodsList[i].total); hPos+=rowHeight; } //商品遍歷打印完畢,空一行 hPos+=rowHeight; //合計(jì) LODOP.ADD_PRINT_TEXT(hPos,80,pageWidth,rowHeight,'合計(jì):'+json.totalCount); LODOP.ADD_PRINT_TEXT(hPos,130,pageWidth,rowHeight,'¥'+json.total); hPos+=rowHeight; LODOP.ADD_PRINT_TEXT(hPos,2,pageWidth,rowHeight,(new Date()).toLocaleDateString()+' '+(new Date()).toLocaleTimeString()) hPos+=rowHeight; LODOP.ADD_PRINT_TEXT(hPos,25,pageWidth,rowHeight,'謝謝惠顧,歡迎下次光臨!'); //初始化打印頁(yè)的規(guī)格 LODOP.SET_PRINT_PAGESIZE(3,pageWidth,45,'XXXXX訂單信息');} });
后來(lái)發(fā)現(xiàn)如果把原生js放到controller的控制域外就可以找到該函數(shù),并且可以正常運(yùn)行(如下)
app.controller(’orderFormCtrl’, function ($scope, $http) { // 訂單備注 $scope.food_remarks = []; // 訂單詳情數(shù)組 $scope.forms_detail = []; $scope.food_detail = []; // 查詢訂單詳情 $scope.search_detail = function(index){ $scope.forms_detail = $scope.food_forms[index];$scope.food_remarks = $scope.food_forms[index].remarks;console.log($scope.forms_detail);// var form_num = $scope.forms_detail.order_number;$scope.food_detail = $scope.food_forms[index].food_detail;// console.log($scope.food_detail);for (var i = 0; i < $scope.food_detail.length; i++) { for (var j = 0; j < $scope.food_menu.length; j++) {if ($scope.food_detail[i].id == $scope.food_menu[j].id) { $scope.food_detail[i].name = $scope.food_menu[j].name; $scope.food_detail[i].price = $scope.food_menu[j].price; $scope.food_detail[i].img = $scope.food_menu[j].img;} }} }; //計(jì)算總價(jià) $scope.allSum=function(){ var allPrice = 0; for(var i= 0;i<$scope.food_detail.length;i++){ allPrice+=$scope.food_detail[i].price*$scope.food_detail[i].num; // console.log(allPrice);} return allPrice; }; });//controller控制域外var LODOP; //聲明為全局變量 function printPreview(){ //創(chuàng)建小票打印頁(yè) CreatePrintPage(); //打印預(yù)覽 LODOP.PREVIEW(); } /** * 樣例函數(shù),服務(wù)器確認(rèn)訂單后執(zhí)行 */ function printOrder() {//創(chuàng)建小票打印頁(yè) CreatePrintPage(); //開(kāi)始打印 LODOP.PRINT(); } function CreatePrintPage(json) { //json 創(chuàng)建模擬服務(wù)器響應(yīng)的訂單信息對(duì)象 var json = {'title':'XXXXX訂單信息', 'name':'張三', 'phone': '138123456789', 'orderTime': '2012-10-11 15:30:15', 'orderNo': '20122157481315', 'shop':'XX連鎖', 'total':25.10,'totalCount':6, 'goodsList':[ {'name':'菜心(無(wú)公害食品)', 'price':5.00, 'count':2, 'total':10.08}, {'name':'菜心(無(wú)公害食品)', 'price':5.00, 'count':2, 'total':10.02}, {'name':'旺菜', 'price':4.50, 'count':1, 'total':4.50}, {'name':'黃心番薯(有機(jī)食品)', 'price':4.50, 'count':1, 'total':4.50} ] } var hPos=10,//小票上邊距 pageWidth=580,//小票寬度 rowHeight=15,//小票行距 //獲取控件對(duì)象 LODOP=getLodop(document.getElementById(’LODOP_OB’),document.getElementById(’LODOP_EM’)); //初始化 LODOP.PRINT_INIT('打印控件功能演示_Lodop功能_名片'); //添加小票標(biāo)題文本 LODOP.ADD_PRINT_TEXT(hPos,30,pageWidth,rowHeight,json.title); //上邊距往下移 hPos+=rowHeight; LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,'姓名:'); LODOP.ADD_PRINT_TEXT(hPos,30,pageWidth,rowHeight,json.name); //hPos+=rowHeight; //電話不換行 LODOP.ADD_PRINT_TEXT(hPos,70,pageWidth,rowHeight,'電話:'); LODOP.ADD_PRINT_TEXT(hPos,100,pageWidth,rowHeight,json.phone); hPos+=rowHeight; LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,'下單時(shí)間:'); LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.orderTime); hPos+=rowHeight; LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,'訂單編號(hào):'); LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.orderNo); hPos+=rowHeight; LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,'取貨門店:'); LODOP.ADD_PRINT_TEXT(hPos,60,pageWidth,rowHeight,json.shop); hPos+=rowHeight; LODOP.ADD_PRINT_LINE(hPos,2, hPos, pageWidth,2, 1); hPos+=5; LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,'商品名稱'); LODOP.ADD_PRINT_TEXT(hPos,70,pageWidth,rowHeight,'單價(jià)'); LODOP.ADD_PRINT_TEXT(hPos,110,pageWidth,rowHeight,'數(shù)量'); LODOP.ADD_PRINT_TEXT(hPos,140,pageWidth,rowHeight,'小計(jì)'); hPos+=rowHeight; //遍歷json的商品數(shù)組 for(var i=0;i<json.goodsList.length;i++){ if(json.goodsList[i].name.length<4){ LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,json.goodsList[i].name); }else { //商品名字過(guò)長(zhǎng),其他字段需要換行 LODOP.ADD_PRINT_TEXT(hPos,1,pageWidth,rowHeight,json.goodsList[i].name); hPos+=rowHeight; } LODOP.ADD_PRINT_TEXT(hPos,70,pageWidth,rowHeight,json.goodsList[i].price); LODOP.ADD_PRINT_TEXT(hPos,115,pageWidth,rowHeight,json.goodsList[i].count); LODOP.ADD_PRINT_TEXT(hPos,140,pageWidth,rowHeight,json.goodsList[i].total); hPos+=rowHeight; } //商品遍歷打印完畢,空一行 hPos+=rowHeight; //合計(jì) LODOP.ADD_PRINT_TEXT(hPos,80,pageWidth,rowHeight,'合計(jì):'+json.totalCount); LODOP.ADD_PRINT_TEXT(hPos,130,pageWidth,rowHeight,'¥'+json.total); hPos+=rowHeight; LODOP.ADD_PRINT_TEXT(hPos,2,pageWidth,rowHeight,(new Date()).toLocaleDateString()+' '+(new Date()).toLocaleTimeString()) hPos+=rowHeight; LODOP.ADD_PRINT_TEXT(hPos,25,pageWidth,rowHeight,'謝謝惠顧,歡迎下次光臨!'); //初始化打印頁(yè)的規(guī)格 LODOP.SET_PRINT_PAGESIZE(3,pageWidth,45,'XXXXX訂單信息');}
請(qǐng)問(wèn)這是什么原因造成的呢?之前做過(guò)的一個(gè)項(xiàng)目在controller中寫原生的js也是可以用的啊,不知道這次為什么不可以
問(wèn)題解答
回答1:基礎(chǔ)性問(wèn)題。
當(dāng)在 use strict 模式下,function 應(yīng)該遵循先定義后使用。
相關(guān)文章:
1. mysql - 如何在有自增id的情況下,讓其他某些字段能不重復(fù)插入2. 求救一下,用新版的phpstudy,數(shù)據(jù)庫(kù)過(guò)段時(shí)間會(huì)消失是什么情況?3. mysql - 請(qǐng)問(wèn)數(shù)據(jù)庫(kù)字段為年月日,傳進(jìn)的參數(shù)為月,怎么查詢那個(gè)月所對(duì)應(yīng)的數(shù)據(jù)4. [python2]local variable referenced before assignment問(wèn)題5. html - 移動(dòng)端radio無(wú)法選中6. javascript - 我的站點(diǎn)貌似被別人克隆了, google 搜索特定文章,除了域名不一樣,其他的都一樣,如何解決?7. javascript - vue+iview upload傳參失敗 跨域問(wèn)題后臺(tái)已經(jīng)解決 仍然報(bào)403,這是怎么回事啊?8. php - 微信開(kāi)發(fā)驗(yàn)證服務(wù)器有效性9. Python2中code.co_kwonlyargcount的等效寫法10. node.js - win 下 npm install 遇到了如下錯(cuò)誤 會(huì)導(dǎo)致 無(wú)法 run dev么?
