[英]ng-submit not working for this plunk
我是angular js的新手,正在构建我的第一个应用程序以在点击github API时显示用户。
这是我正在研究的小品: http ://plnkr.co/edit/bJxijtHV4kBmJ3heMxA9? p=preview
<form name="searchUser" ng-submit="Search(userName)">
<input type="search" placeholder="User to find" ng-model="userName"/>
<input type="submit" value="Search" />
</form>
JavaScript代码:
var app = angular.module('myApp', []);
app.controller('MainController', function($scope, $http) {
var Search = function(userName) {
$http.get('https://api.github.com/users/' + userName)
.then(function(response) {
$scope.person = response.data;
});
};
});
请让我知道我要去哪里了。 这是我的第一个应用程序。 对不起任何愚蠢的错误:)
您需要将Submit函数绑定到scope属性,因为普通函数,变量无法在html中访问
喜欢
$scope.Search = function(userName) {
$http.get('https://api.github.com/users/' + userName)
.then(function(response) {
$scope.person = response.data;
});
};
});
这是更新的朋克
它应该像下面。
$scope.Search = function(userName){
//rest of the code goes here
}
首先,尝试学习如何在angularJS控制器中编写函数。 您正在将搜索定义为变量对象而不是函数。
尝试这个 -
$scope.search = function(userName) {
$http.get('https://api.github.com/users/' + userName)
.then(function(response) {
$scope.person = response.data;
});
};
如果您不熟悉angularJS,请从这里学习...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.