angular-data bindOne using "controller as" syntax

93 Views Asked by At

How can I bind an angular-data model via Model.bindOne() to a scope when using controller as syntax?

This works if I inject $scope:

MyModel.bindOne( $scope, 'myModel', myId );  // Works as expected

But when using controller as, I would almost expect to be able to do the following:

MyModel.bindOne( this, 'myModel', myId );  // TypeError: undefined is not a function
1

There are 1 best solutions below

0
Louie Almeda On

I had the same problem, this worked for me. Referencing the solution provided by the author of js-data in this issue:

//using UserCtrl as vm
.controller('UserCtrl', function ($scope, MyModel) {
  MyModel.bindOne($scope, myId, 'vm.myModel');
});

myModel is now available in your controller.