我试图根据名称进行搜索,但它无法正常工作。以下是我使用的代码:
用于显示的HTML代码:
<div ng-controller="PhoneBookUserCtrl as pu">
<h2>User controller to get the User details</h2>
<span>Enter name in the textbox to get details</span>
<input type="text" ng-model="pu.name" />
<button ng-click="pu.get(pu.name)">Get</button>
<p>User Information</p>
<p><b>Name : </b> {{pu.userInfo.name}}</p>
<p><b>Email : </b> {{pu.userInfo.email}}</p>
<p><b>Number : </b> {{pu.userInfo.number}}</p>
</div>
Angular Code:
内部服务 - 搜索功能:
this.getInfo=function(name){
var info= "";
for(var i=0; i<contacts.lenght;i++){
if(contacts[i].name.indexOf(name) >= 0){
info = contacts[i];
console.log(info);
break;
} else{ alert("Not found");}
}
console.log("inside getInfo"); //its getting executed
return info;
}
控制器内的代码调用函数
app.controller("PhoneBookUserCtrl",['contacts', function(contacts){
this.get=function(name) {
this.userInfo = contacts.getInfo(name);
console.log("Inside User Controller"); //its getting executed
}
}]);
如何让它发挥作用,需要做哪些改变?