2014-02-13 51 views
-2

我从网上得到了一个示例应用程序,我试图实现,但它似乎并没有工作,下面是应用程序的代码,请帮我修复它并建议我我怎样才能让它变得更好。我是Angular的新手。角JS不似乎工作,新手

的jsfiddle:http://jsfiddle.net/eGzZg/

<div id="main" ng-controller="contactDetails"> 
     <label>Name</label> 
     <input type="text" name="name" ng-model="newcontact.name"/> 
     <label>Email</label> 
     <input type="text" name="email" ng-model="newcontact.email"/> 
     <label>Phone No</label> 
     <input type="text" name="phno" ng-model="newcontact.phno"/> 
     <input type="hidden" ng-model="newcontact.id" /> 
     <input type="button" value="Save" ng-click="saveContact()" /> 
    <div id="table"> 
    <div id="thead"> 
     <span>Name</span> 
     <span>Email</span> 
     <span>Phone No</span> 
     <span>Action</span> 
    </div> 
    <div id="tbody" ng-repeat="contact in contacts"> 
     <span> {{contact.name}} </span> 
     <span> {{contact.email}} </span> 
     <span> {{contact.phno}} </span> 
     <span> <a href='#' ng-click="edit(contact.id)">Edit</a></span> 
     <span> <a href='#' ng-click="delete(contact.id)">Delete</a></span> 
    </div> 
    </div> 
</div> 


<script> 
var uid = 0; 
function contactDetails($scope) { 
    //$scope.contacts = [{ id : null, 'name':null, 'phno':null }]; 
    $scope.contacts = [ 
     { id:0, 'name': 'Viral', 
      'email':'[email protected]', 
      'phno': '123-2343-44' 
     } 
    ]; 
    $scope.saveContact = function(){ 
    alert('sd'); 
    for(i in $scope.contacts){ 
     if($scope.contacts[i].id == $scope.newcontact.id || ($scope.contacts[i].name == $scope.newcontact.name && $scope.contacts[i].phno == $scope.newcontact.phno)){ 
     $scope.contacts[i] = $scope.newcontact; 
     }else{ 
     $scope.contacts.id = ++uid; 
     $scope.contacts.push($scope.newcontact); 
     } 
     $scope.newcontact = {}; 
    } 
    } 
} 
</script> 
+0

您如何期待这项工作? Angular甚至没有加载。使用有角模板的plunker来玩它。 –

回答

1

不要忘了其实是包括角度,你也忘了NG-应用

<body ng-app> 

here ya go

+0

感谢Buddy!我在这里有一个问题,请检查jsfiddle中的代码,问题最初是我将联系人对象设置为空,因为您在开始时总是看到额外的编辑和删除标签,如何摆脱它? http://jsfiddle.net/eGzZg/4/ –

+0

我不是很了解你的问题。我强烈建议你首先浏览AngularJS教程:http://docs.angularjs.org/tutorial(如果你还没有的话)。 然后通过所有25天这个:http://www.ngnewsletter.com/advent2013/#!/ 我承诺经过两个,你将成为一个角色专家在任何时候。 – xyclos

0

只要改变$scope.contacts = [{ id : null, 'name':null, 'phno':null }];

$scope.contacts = [];

即使您将联系人元素的属性设置为null,它仍然具有单个元素,这就是为什么没有文本,但按钮在页面加载时显示。