0

{{myItem}}为什么当我尝试分配一个我得到的新变量或输出到我得到的控制台时,我从我的 with 但在我的控制器中获取数据绑定的值undefined

这是我的控制器:

var controllers = angular.module("myItem.controllers", []);
controllers.controller('myItemController', function ($scope, MyItemService) {
    $scope.version_model = null;
    $scope.itemGroup_model = null;
    $scope.item_model = null;

    $scope.myItem = null;
    $scope.versions = null;
    $scope.itemGroups = null;
    $scope.hierarchies = null;

    $scope.init = function () {

        $scope.myItem = MyItemService.getMyItem();
        console.log("myItem => id= " + $scope.myItem.versionID + " requestTypeID: " + $scope.myItem.requestTypeID);
        $scope.versions = MyItemService.getVersions();
        $scope.version_model = $scope.myItem.versionID;
        console.log("version_model :" + $scope.version_model + "scope.myItem.versionID : " + $scope.myItem.versionID);

        $scope.itemGroups = MyItemService.getItemGroups();
        $scope.itemGroup_model = $scope.myItem.itemGroup;
        console.log("itemGroup_model :" + $scope.itemGroup_model + " scope.myItem.itemGroup : " + $scope.myItem.itemGroup);

        $scope.hierarchies = MyItemService.getItemList($scope.itemGroup_model);
        $scope.item_model = $scope.myItem.itemName;
        console.log("item_model :" + $scope.item_model + " scope.myItem.itemName : " + $scope.myItem.itemName);
    }

    $scope.getItems = function (group) {
        $scope.hierarchies = MyItemService.getHierarchyList(group);
    }

每当我尝试从 $scope.myitem 分配任何东西时,我都会一直不确定。我已经尝试在 init 内部进行设置,在 init 外部进行设置。除了_model值未定义的地方,一切都在我的页面上运行,一旦我与 someting 交互并且它得到更新,一切正常,只是在初始化时没有

更新

这就是我的服务的样子

angular.module('servicefactories', [])
    .factory('MyItemService', function ($http, $q) {
    obj.getMyItem= function () {
        var mypromise = $q.defer();
        $http.get('getforEdit')
            .success(function (data, status, headers, config) {
            mypromise.resolve(data);
        });

        return mypromise.promise;
    }

    return obj;

});
4

2 回答 2

1

调用你的函数: $scope.init();

在您的代码中,init 函数不要在初始化控制器上运行。您必须手动调用它。

于 2013-08-23T11:57:34.867 回答
1

正如我怀疑这是异步调用的问题。您可以使用thenpromise 的方法来访问数据并在数据来自服务器后进行分配。就像是

MyItemService.getMyItem().then(function(data) {
         $scope.myItem=data;
         console.log("myItem => id= " + $scope.myItem.versionID + " requestTypeID: " + $scope.myItem.requestTypeID);
        $scope.versions = MyItemService.getVersions();
        $scope.version_model = $scope.myItem.versionID;
        console.log("version_model :" + $scope.version_model + "scope.myItem.versionID : " + $scope.myItem.versionID);
});
于 2013-08-23T12:55:50.937 回答