【发布时间】:2016-02-05 18:14:40
【问题描述】:
我正在使用以下内容,经过多次挫折和谷歌搜索,我无法在 $resource for Marketplace.buy 成功加载后更新 $scope 中的 data_banks 项目;
myApp.directive('marketplaceBuy', function() {
return {
restrict: 'A',
controller: ['$scope', "$timeout", "Marketplace", "Notification", "DataBank", function ($scope, $timeout, Marketplace, Notification, DataBank) {
$scope.dooo = function(attrs) {
Marketplace.buy({'item': attrs.itemId},
function(){
DataBank.query({word: "databanks"}, function(data){
$scope.data_bank = data.data;
});
}, function(resp){
Notification.error(resp.data.message);
});
}
}],
link: function ($scope, element, attrs) {
element.bind('click', function () {
$scope.dooo(attrs);
});
}
};
});
使用$scope.$apply() 错误。
编辑
原来的data_bank是这样加载的
myControllers.controller('LocationListCtrl', ['$scope', 'DataBank', function($scope, DataBank) {
$scope.data_bank = DataBank.query({word: "databanks"});
});
它绑定到这样的视图
<body ng-cloak>
<div ng-view>
<div ng-repeat="blue_print in data_bank.blue_prints">{{ blue_print.name }}</div>
</div>
</body>
最后,控制器使用路由加载
myApp.config(['$routeProvider', '$httpProvider',
function($routeProvider, $httpProvider) {
$httpProvider.interceptors.push('MyInterceptor');
$routeProvider.
when('/locations', {
templateUrl: "/bundles/app/partials/locations.html",
controller: 'LocationListCtrl'
})
}]);
【问题讨论】:
-
你不能将
$scope.doo函数放在链接函数中并完全避免控制器选项吗? -
我认为他想注入 Marketplace、DataBank 和 Notification 并使用它们
-
能否提供给我们查看代码?或者视图如何访问这个范围?
-
谢谢,查看我的编辑信息
标签: angularjs angularjs-directive angularjs-scope angularjs-resource