I have weird "non-error" in my restangular setup. Stuff works fine even though errors are logged to the console.
I use a component driven approach with a decoupled restangular service factory (clientService). The service factory is injected into the controller and bound to this.clients = clientService;
In the clientrecord widget's link function I get a specific client and watch for changes to client.keywords
done by another directive.
link: function (scope, element, attrs, ctrl) {
var cid = $stateParams.clientid;
scope.client = ctrl.clients.one(cid).get().$object;
scope.$watchCollection('client.keywords',
function (newval, oldval, scope) {
scope.client.put();
}
);
}
The code works fine. Keywords are edited by the other widget, changes are detected, put requests are made and keywords end up in the database. But both chrome and firefox throw an error:
"typeError: scope.client.put" is not a function"
At the moment it is just an annoyance, but could it evolve into nasty behaviour further on? Can I fix it? Do I need to typecast something?
The Service itself is basically a one-liner:
(function () {
'use strict';
angular
.module('client')
.factory('clientService', ClientService);
ClientService.$inject = ['Restangular','$stateParams'];
function ClientService(Restangular) {
return Restangular.service('routes/clients');
}
}());
答案 0 :(得分:0)
尝试在观察者的scope.client
添加存在检查(不是很好的解决方案):
function (newval, oldval, scope) {
scope.client = scope.client ? scope.client : [];
scope.client.put();
}
或scope.client
之前创建(稍好一点的解决方案):
scope.client = [];
scope.client = ctrl.clients.one(cid).get().$object;
<强>更新强>
或者(更好的方法),如果ctrl.clients
返回promise,则在数据加载后创建观察者。像这样:
ctrl.clients.one(cid).get().then(function(data){
scope.client = data;
scope.$watchCollection(/* watcher; */);
});
但这只是猜想,因为我不知道ctrl.clients
结构;