Angular js无法分配范围

时间:2016-02-22 21:20:00

标签: javascript angularjs node.js electron

我正在使用electron.js,我有一个自定义node.js模块,它使用fs模块搜索文件中的单词。

我使用angular.js将文件搜索的结果保存在$ scope中,但范围值始终返回undefined。

这是角度代码:

var fs = require('fs');
var readline = require('readline');

var reader = readline.createInterface({
    input: fs.createReadStream('testfile.txt'),
});


var word = function DefineWord() {
    this.reader = reader;
}
var search = defineWord.prototype.searchWord = function(search, cb) {
    var arr = [];
    this.reader.on('line', function(line) {

        if (line.search(searchTerm) !== -1) {
            arr.push(line);
        }
    });

    this.reader.on('close', function() {
        cb(arr)
    })

}

exports.word = word;

它不会返回arr变量。

以下是代码的所有相关位:

的node.js:

var searchApp = angular.module("searchApp", []);
var searchMod = require("./search");

searchApp.controller("userWordController", function($scope, GetSearchService) {
    $scope.getDefinition = function() {
        GetSearchService.findWord()
            .searchWord('apple', function(arr) {
              console.log('this line of code never gets executed');
              $scope.wordsResult = arr;
            });
    }
});

searchApp.service('GetSearchService', function() {
    this.findWord = function() {
        return new searchMod.word;
    }
})

angular.js:

<html>
<head>
 <meta charset="UTF-8">
 <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> </script>
 <script src = "app.js"></script>
 </head>
 <body>
 <div ng-app="searchApp" ng-controller="userWordController">
      <button ng-click="getDefinition()"> click here</button>
      <p>{{wordsResult}}</p>
 </div>
 </body>
</html>

HTML

float scale = (float)metrics.densityDpi / (float)DisplayMetrics.DENSITY_HIGH;

imageWidth = (int) (options.outWidth * scale + 0.5);
imageHeight = (int) (options.outHeight * scale + 0.5);

0 个答案:

没有答案