我在下面提供了一个代表此问题的小片段。
var app = angular.module("app", []);
app.controller("AppCtrl", function ($http, $scope) {
$scope.fetchData = function () {
document.getElementById("displayBlock").className='hidden';
document.getElementById("throbber").className='throbber';
document.getElementById("intermediate").className='unhidden';
document.getElementById("header").className='step';
var app = this;
var p= $scope.formUsernameTex;
alert(p);
if(p!="undifined"){
$http.get("https://api.github.com/users/"+$scope.formUsernameText)
.success(function (data) {
app.user = data;
$scope.name=app.user.name;
$scope.avatar_url=app.user.avatar_url;
if (app.user.html_url==null) {$scope.html_url="Not Shared";} else {$scope.html_url=app.user.html_url};
if (app.user.company==null||app.user.company=="") {$scope.company="Not Shared";} else {$scope.company=app.user.company};
if (app.user.blog==null) {$scope.website="Not Shared";$scope.websiteLink="";} else {$scope.website=app.user.blog;$scope.websiteLink=$scope.website;};
if (app.user.location==null) {$scope.location="Not Shared";} else {$scope.location=app.user.location;};
if (app.user.email==null) {$scope.email="Not Shared";$scope.emailLink="";} else {$scope.email=app.user.email;$scope.emailLink="mailto:"+$scope.email;};
if (app.user.hireable) {var hireStatus="Yes"} else {var hireStatus="No"};
$scope.hireable=hireStatus;
$scope.public_repos=app.user.public_repos;
$scope.public_gists=app.user.public_gists;
$scope.followers=app.user.followers;
$scope.following=app.user.following;
var date = new Date(app.user.created_at);
$scope.created_at=date.getDate()+"/"+date.getMonth()+"/"+date.getFullYear();
//Display Block Functions
document.getElementById("throbber").className='hidden';
document.getElementById("displayBlock").className='unhidden';
document.getElementById("header").className='';
})
.error(function () {
document.getElementById("intermediate").className='hidden';
document.getElementById("header").className='initial';
alert("Hmm.... That doesn't look quite right!\n\nOctoStats couldn't find the User's data on GitHub! :-/")
})
}
else {alert("Add name properly");}
}
})
假设在这里p=undifined
我只想提供一个警报,那个时候其余功能将无法使用。
请告诉我该怎么做。
这里我不能正确使用
答案 0 :(得分:0)
对您的问题的简短回答是将您的if
声明更改为:
if(p != undefined)
您可以修复“未定义”的拼写,并删除引号。
像charlietti所说,更长的答案是在潜入更重的应用程序之前真正掌握角度的基础知识。蝙蝠,我看到一些不会进入最终版本的东西:
getElementById()
。相反,您将通过$scope
更新模型,然后让角度更改您的用户界面。首先,请查看ngShow,ngHide和ngClass,看看如何做到这一点。app.user
,然后将剩余的内容工作,而不是将所有$scope
属性复制到$scope.app.user = app.user
。我建议您查看this SO post以便更好地理解。
答案 1 :(得分:0)
我认为您的源代码或转录到Stack Overflow可能会有几个拼写错误。
您在t
末尾错过了$scope.formUsernameTex
,错误拼写了undefined
,而且您不需要在undefined
周围放置双引号以下几行:
var p= $scope.formUsernameTex;
alert(p);
if(p!="undifined"){
$http.get("https://api.github.com/users/"+$scope.formUsernameText)
应该是:
var p = $scope.formUsernameText;
alert(p);
if(p != undefined){
$http.get("https://api.github.com/users/"+$scope.formUsernameText)