dpope
dpope

Reputation: 63

angularjs with $scope data-binding works just once

i've a form with a select box. It's populate with data from JSON where each row has many keys. My idea is to show Description field on select, track by ID and other values showed in label.

This is my code:

                <div>
                    <label>Level:</label>
                    <div><select ng-model="levelModel" ng-options="level.code for level in levels track by id" required></select></div>
                    <label>name:{{levelModel.name}}</label>
                    <label>desc:{{levelModel.desc}}</label>
                </div>

and this is JSON

    levels = [
        {id:1, code:"a-code", name:"a-name", desc:"a-desc"},
        {id:2, code:"b-code", name:"b-name", desc:"b-desc"},
        {id:3, code:"c-code", name:"c-name", desc:"c-desc"} ]

My problem is on changing data on select box, data-binding works just once assigning to label the init data and not the selected data, all the others times binding doesn't work.

I think the reason is that i'm injecting $scope. I've read some guide about using $scope.$apply but if i try to user $scope.$apply in my controller like this

$scope.$apply(function(){
    $scope.levelModel.name = "test"; })

after a few seconds i receive $rootScope:inprogr error

This is my app.js with route

app.config(['$routeProvider',function($routeProvider){
        $routeProvider
            .when("/report",{
                          templateUrl:"/reportV.html",
                          controller:"reportController",
                          resolve:{
                                   levels: function(selectSrv){
                                       return selectSrv.select(table); } } })
            .otherwise({redirectTo:'/'});

and this is my controller

app.controller("reportController", ["$scope", "levels", function($scope,levels){

    $scope.levels = levels.data;

How can i resolve?

P.S. my code is simplified, the view is routed and inside resolve i run some services to receive data from db

Upvotes: 0

Views: 57

Answers (0)

Related Questions