Syd*_*ney 28 angularjs angular-ui angular-ui-router
我想显示一个表格,其中包含与编辑项目相对应的数据.我ui-router用于路由.我定义了一个州:
myapp.config(function($stateProvider) {
$stateProvider.
.state('layout.propertyedit', {
url: "/properties/:propertyId",
views : {
"contentView@": {
templateUrl : 'partials/content2.html',
controller: 'PropertyController'
}
}
});
Run Code Online (Sandbox Code Playgroud)
在PropertyController,我想设置$scope.property来自以下呼叫的数据(Google Cloud Endpoints):
gapi.client.realestate.get(propertyId).execute(function(resp) {
console.log(resp);
});
Run Code Online (Sandbox Code Playgroud)
我不知道是否可以使用,resolve因为数据是异步返回的.我试过了
resolve: {
propertyData: function() {
return gapi.client.realestate.get(propertyId).execute(function(resp) {
console.log(resp);
});
}
}
Run Code Online (Sandbox Code Playgroud)
第一个问题,propertyId未定义.你怎么得到propertyId的url: "/properties/:propertyId"?
基本上我想设置$scope.property在PropertyController向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;
}
});
Run Code Online (Sandbox Code Playgroud)
Nat*_*ele 53
您需要阅读文档以求解决.Resolve函数是可注入的,您可以使用$stateParams从路由中获取正确的值,如下所示:
resolve: {
propertyData: function($stateParams, $q) {
// The gapi.client.realestate object should really be wrapped in an
// injectable service for testability...
var deferred = $q.defer();
gapi.client.realestate.get($stateParams.propertyId).execute(function(r) {
deferred.resolve(r);
});
return deferred.promise;
}
}
Run Code Online (Sandbox Code Playgroud)
最后,一旦解析,解析函数的值可在控制器中注入:
myapp.controller('PropertyController', function($scope, propertyData) {
$scope.property = propertyData;
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
41628 次 |
| 最近记录: |