Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在angular中检索javascript控制器上的输入变量?_Javascript_Html_Angularjs - Fatal编程技术网

如何在angular中检索javascript控制器上的输入变量?

如何在angular中检索javascript控制器上的输入变量?,javascript,html,angularjs,Javascript,Html,Angularjs,我有一个搜索输入字段,我试图将该输入发送到我想要使用的JavaScript变量。这是角形的 输入 <input ng-model="searchText" placeholder="Search"> 我的JavaScript文件与HTML文件位于不同的位置 非常感谢。您可以在AngularJS应用程序中实现您的逻辑,在Github上搜索 在searchOnGithub()方法中,您可以使用该方法调用PHP脚本,传递变量$scope.searchText的值,然后在视图中显示结果

我有一个搜索输入字段,我试图将该输入发送到我想要使用的JavaScript变量。这是角形的

输入

<input ng-model="searchText" placeholder="Search">

我的JavaScript文件与HTML文件位于不同的位置


非常感谢。

您可以在AngularJS应用程序中实现您的逻辑,在Github上搜索

searchOnGithub()
方法中,您可以使用该方法调用PHP脚本,传递变量
$scope.searchText的值,然后在视图中显示结果

希望这有助于开始:

angular
.module('myApp',[])
.controller('myController',函数($scope){
$scope.searchOnGithub=函数(){
//在Github上执行搜索的逻辑是什么
console.clear();
log($scope.searchText);
};
});

您可以这样做

var-app=angular.module(“sampleApp”,[]);
app.controller(“sampleController”,[“$scope”,
职能($范围){
$scope.searchText=“你好”;
$scope.search=函数(){
log($scope.searchText);
}
}
]);

搜寻

您的代码很好。如果您手动向
$scope
添加
searchText
属性,或者您仅依赖
ng model=“searchText”
(这也会自动向
$scope
添加属性),则控制器将使用
$scope.searchText
访问绑定的
值:

angular.module('searchkingApp')
  .controller('MainCtrl', function($scope){
       var searchedItem = $scope.searchText;
  });
现在,您可以在用户执行某些事件(例如,单击事件)时执行操作,也可以查看
$scope.searchText
以了解更改:

<input type="text" ng-click="search()">

$scope.search = function() {
    // $scope.searchText will give the search terms
};

$scope.search=函数(){
//$scope.searchText将提供搜索条件
};
…或:

// You would go this way if you want to implement something like 
// Google Instant Search so you don't need to click anything, you perform
// searches whenever the <input> changes
$scope.$watch(function() { return $scope.searchText; }, function(newValue, oldValue) {
    // newValue contains current $scope.searchText value
});
//如果您想实现类似于
//谷歌即时搜索,所以你不需要点击任何东西,你执行
//每当更改时进行搜索
$scope.$watch(函数(){return$scope.searchText;},函数(newValue,oldValue){
//newValue包含当前$scope.searchText值
});

angular应用程序之外的javascript中访问变量的具体原因是什么?或者是angular应用程序中的,但来自不同的控制器或其他什么我想在github存储库上执行搜索,我可以使用硬编码变量来执行搜索。但现在我正在尝试进行搜索,并努力将变量获取到JavaScript文件中,以便使用它。假设这是一个简单的搜索,但我遗漏了一些东西。您只需在控制器中使用$scope.searchText来访问该值。这就像在HTML表单中使用输入,然后将该“字符串”或插入项推送到另一个页面中使用它一样。我可以用PHP来做。但是angular在控制器中有$scope的东西,我在那里迷路了。我现在明白了。当您指的是另一个文件中的javascript变量时,我感到困惑。这很简单。你可以参考我的答案
// You would go this way if you want to implement something like 
// Google Instant Search so you don't need to click anything, you perform
// searches whenever the <input> changes
$scope.$watch(function() { return $scope.searchText; }, function(newValue, oldValue) {
    // newValue contains current $scope.searchText value
});