为什么angularJS不绑定数据?

时间:2016-10-14 18:40:16

标签: javascript angularjs json

我从Employee表中获取最高工资。 JSON正确检索数据,但为什么angular不绑定数据?

Controller.js

app.controller('AdminCntrl', function ($scope, AdminServices) {
    $scope.BtnSubmit = function () {
        alert('ok1')
        var Bust = AdminServices.Getmax();

        Bust.then(function (d) {
            $scope.Emp = d.data;

            })
        }
})

Service.js

app.service('AdminServices', function ($http) {

    this.Getmax = function () {
        var ss = $http({
            url: '/Department/Max',
            method: 'Get',
            data:JSON.stringify(),
            content:{'content-type' : 'application/Json'}
        })
        return ss;
    }
})

Controller

public JsonResult Max()
        {
               var xxx = db.Employees.OrderByDescending(ww => ww.Salary).First();
            return new JsonResult { Data = xxx, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

Emp.cshtml

<div ng-controller="AdminCntrl">
        <input type="button" class="btn btn-primary btn-sm" value="Min" ng-click="BtnSubmit()" />

        <div ng-repeat="Accessor in Emp">
            {{Accessor.Id}}
            <span style="color:red">{{Accessor.Name}}</span>
        </div>
    </div>

Fiddler Catch Data

{"Id":11,"Name":"Saleem","Department":"Oracle","Salary":100000.00,"EmpType":"Admin","DataofJoin":"\/Date(1462127400000)\/","DataofBirth":"\/Date(597868200000)\/"}

1 个答案:

答案 0 :(得分:2)

$scope.Emp的值是否等于您的提琴手数据?

看来你没有返回一个ng-repeat正在期待的数组。如果您的Fiddler数据表示为数组,我会打赌:

[{"Id":11,"Name":"Saleem","Department":"Oracle","Salary":100000.00,"EmpType":"Admin","DataofJoin":"\/Date(1462127400000)\/","DataofBirth":"\/Date(597868200000)\/"}] 

你看到它绑定了。

或者,如果你真的不期待一个数组,那么如果你替换

<div ng-repeat="Accessor in Emp">
     {{Accessor.Id}}
     <span style="color:red">{{Accessor.Name}}</span>
</div>

使用:

<div>
      {{Emp.Id}}
       <span style="color:red">{{Emp.Name}}</span>
</div>

你取得了一些成功