【发布时间】:2013-08-02 23:32:08
【问题描述】:
我想显示一个包含与编辑项目对应的数据的表单。我使用ui-router 进行路由。我定义了一个状态:
myapp.config(function($stateProvider) {
$stateProvider.
.state('layout.propertyedit', {
url: "/properties/:propertyId",
views : {
"contentView@": {
templateUrl : 'partials/content2.html',
controller: 'PropertyController'
}
}
});
在PropertyController 中,我想使用来自以下调用(Google Cloud Endpoints)的数据设置$scope.property:
gapi.client.realestate.get(propertyId).execute(function(resp) {
console.log(resp);
});
不知道能不能用resolve,因为数据是异步返回的。我试过了
resolve: {
propertyData: function() {
return gapi.client.realestate.get(propertyId).execute(function(resp) {
console.log(resp);
});
}
}
第一个问题,propertyId 未定义。如何从url: "/properties/:propertyId" 获得propertyId?
基本上我想将PropertyController 中的$scope.property 设置为异步调用返回的resp 对象。
编辑:
myapp.controller('PropertyController', function($scope, , $stateParams, $q) {
$scope.property = {};
$scope.create = function(property) {
}
$scope.update = function(property) {
}
function loadData() {
var deferred = $q.defer();
gapi.client.realestate.get({'id': '11'}).execute(function(resp) {
deferred.resolve(resp);
});
$scope.property = deferred.promise;
}
});
【问题讨论】:
-
请添加一个jsfiddle。
标签: angularjs angular-ui angular-ui-router