0

On index.html page I have following controller declaration:

<div ng-controller="UserDataController"/>

I need this declaration because somewhere in my templates I have following code (I know it is a bad style, but ...):

<script>

    var userData = angular.element('[ng-controller=UserDataController]').scope().userData;
...
 </script>

But at the same time I'm working with resources(REST) and have code similiar to this one. Which throws in my case error: Unknown provider: dataProvider <- data, because my controller looks like:

function UserDataController($scope,data)
{
    $scope.userData = data;
}

I've read here that this is because of ng-controller declaration. And of course when I removed this declaration the part of code between script tags in my templates is broken. So, I'm in controversial situation -- when I repair one thing, another get broken...

I'm not strong in js and jquery, but the easiest way seems to me is to get somehow controller scope without this selector '[ng-controller=UserDataController]'. How can it be done? Is it a right approach?

Thanks in advance!

Community
  • 1
  • 1
Sharov
  • 458
  • 8
  • 38

2 Answers2

0

I think you're trying to access your userData object all wrong. To access the userData object, simply do a {{userData}} somewhere on your page that is within the scope of your controller. For instance

<div ng-app>
    <div ng-controller="UserDataController">
        <input type="text" ng-model="userData" />
        <div ng-model="userData">User data is: {{userData}}</div>
    </div>
</div>

and then your JS

function UserDataController($scope)
{
    $scope.userData = '';
}

check this fiddle: http://jsfiddle.net/N4MQ4/2/

Ronnie
  • 11,138
  • 21
  • 78
  • 140
  • Thank you for reply, Ronnie! I'm using kendo-ui within angularjs, and I have to prepare datasource for controls, that is why I have to get scope of control. There are some issues with angular-kendo framework within treeview control, so it is temporary solution. – Sharov Sep 02 '13 at 06:34
0

the error:

Unknown provider: dataProvider <- data.... 

implies that you're trying to use angular's dependency injection to inject the data param for you. From the error, I'm guessing there is no provider configured for data. Hence, the error you're seeing. like ronnie, it does seems a bit strange as to why you're doing it the way you are doing

Dziad Borowy
  • 12,368
  • 4
  • 41
  • 53
rickhuynh
  • 1,176
  • 1
  • 8
  • 5