AngularJs简单编辑

时间:2016-07-25 20:27:00

标签: angularjs

我正在按照教程来了解angularjs的基本知识。在我的情况下,编辑链接不会将对象属性加载回文本框。请让我知道我在做错的是代码和小提琴

<body>
<div class="scope" data-ng-app="mymodule" data-ng-controller="mycontroller">
    <h3>AngularJS Filter data sample </h3>
    <br />
    Name:<br /> 
    <input type="text" data-ng-model="Name" /><br />
    <input type="text" data-ng-model="Position" /><br />
    <button data-ng-click="addfriend()"> Add Friend</button>
    <br />
    <input type="text" data-ng-model="Namesearch" /><br />
    <ul>
        <li class="li" data-ng-repeat="element in friendlist | filter:Namesearch | orderBy:'Name'">
            <strong> [{{$index + 1}}] {{ element.Name | uppercase}} working as {{ element.Position}}  </strong>
            [ <a href="#" ng-click="clearUser(element)">clear</a>
            | <a href="#" ng-click="removeUser(element)">X</a>
            | <a href="#" ng-click="editUser(1)">edit</a>
            ]
        </li>
    </ul>
</div>
</body>

和JS代码

var mymodule = angular.module('mymodule', [])
mymodule.controller('mycontroller', ['$scope', function ($scope) {
    $scope.friendlist =
        [{ Name: 'Zia', Position: 'AM' }, { Name: 'Zia1', Position: 'PM' }, { Name: 'Zia2', Position: 'GM' }
        ];

    $scope.editUser = function (id) {

        for (i in $scope.friendlist) {
            if ($scope.friendlist[i].Name == 'Zia') {

                $scope.newFriend = angular.copy($scope.friendlist[i]);
            }
        }
    }
}]);

fiddle

我也希望了解class="scope"

div的重要性

1 个答案:

答案 0 :(得分:0)

这会解决您的问题吗? http://jsfiddle.net/3hv7y369/

在输入中,您需要newFriend.NamenewFriend.Position来显示复制的值。我还完成了editUser()函数以与每个用户一起工作。