为什么我在控制器中的变量没有定义,但它在具有数据绑定的HTML页面中正确显示
Why am I getting undefined for my variable in the controller but it displays correctly in the HTML page with data binding
为什么我使用{{myItem}}
从我的控制器中获取具有数据绑定的值,但当我尝试将我获取或输出的新变量分配给控制台时,我会获得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
值未定义的地方外,我的页面上所有的东西都能正常工作,一旦我与某人交互并更新了它,一切都很好,只是在初始化时没有
更新
这就是我的服务
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;
});
我怀疑这是异步调用的问题。您可以在promise中使用then
方法来访问数据,并在数据来自服务器时进行分配。类似的东西
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);
});
调用您的函数:$scope.init();
在您的代码中,init函数不在初始化控制器上运行。您必须手动调用它。
相关文章:
- 在href跳转到另一个html元素之前,执行Knockout.js数据绑定:点击函数
- AngularJS:将数据绑定到动态创建的 HTML
- NodeJS与AngularJS,所需的HTML文件不是跨控制器的数据绑定全局变量
- ng-指令在ng-Controller之前被调用,将数据绑定到HTML
- HTML 元素的 “dir” 属性 (rtl/ltr) 的数据绑定
- 使用 Directive 通过指令注入 HTML,使用 AngularJS 进行数据绑定
- 为什么我在控制器中的变量没有定义,但它在具有数据绑定的HTML页面中正确显示
- 使用jQuery将数据绑定到html
- Html数据绑定完成
- 在html表的foreach绑定中,是否有可能将数据绑定到一个带有knockout的select下拉列表?
- 如何将服务器生成的html数据绑定到knockout模型属性
- 如何在Mvc帮助器中使用Knockout's数据绑定属性,如Html.EditorFor()
- angularjs中html内容的双向数据绑定
- 在聚合物中注入html,包括数据绑定
- AngularJS:生成空HTML标签的数据绑定(Django冲突)
- AngularJS - html select的数据绑定默认值
- 获取html表中按钮的数据绑定值
- 如何使用AngularJS将html输入标记中的数据绑定到javascript对象
- 如何在不使用html包装的情况下将视图模型数据绑定到Kendo DropDownlist
- 将数据绑定HTML类链接到指令