2014-06-12 13 views
13

Próba wyświetlenia wartości kolumny z kolekcji grid opartej na innej wartości w tym samym wierszu. Użytkownik może wybierać/zmieniać wartości w modalu, który zawiera siatkę z wartościami. Kiedy modal się zamyka, wartości są przekazywane z powrotem. W tym momencie chciałbym ustawić wartość dla „znany również jako”:Błąd: [ngModel: nonassign] Wyrażenie jest nieprzenoszalne

html:

Also known as: <input type="text" `ng-model="displayValue(displayNameData[0].show,displayNameData[0].value)">` 

stworzyłem funkcję w zakresie, aby wybrać wartość tylko wtedy, gdy wartość „Show” jest prawdziwe:

$scope.displayValue = function (show, val) { 
    if (show) { 
     return val; 
    } 
    else { 
     return ''; 
    } 
} 

jednak kiedy zamknąć modal pojawia się błąd:

Error: [ngModel:nonassign] Expression 'displayValue(displayNameData[0].show,displayNameData[0].value)' is non-assignable. 

odniesienie plnkr: http://plnkr.co/edit/UoQHYwAxwdvX0qx7JFVW?p=preview

+2

Tylko dla informacji. Ten błąd wystąpi również w przypadku nazw modeli bez obudowy wielbłąda. to znaczy; ng-model = "employee-name" przejdzie przez błąd, gdzie jako ng-model = "employeeName" nie ma problemu. –

Odpowiedz

12

Jak wspomniano HackedByChinese, nie można wiązać NG-model funkcji, więc spróbuj tak:

<input type="text" ng-if="displayNameData[0].show" 
        ng-model="displayNameData[0].value"> 

Albo jeśli chcesz tej kontroli mają być widoczne można utworzyć dyrektywę, aby dodać funkcję $parsers że ustawi pustą wartość według show:

angular.module('yourModule').directive('bindIf', function() { 
     return { 
      restrict: 'A', 
      require: 'ngModel', 

      link: function(scope, element, attrs, ngModel) { 
       function parser(value) { 
        var show = scope.$eval(attrs.bindIf); 
        return show ? value: ''; 
       } 

       ngModel.$parsers.push(parser); 
      } 
     }; 
    }); 

HTML:

<input type="text" bind-if="displayNameData[0].show" 
        ng-model="displayNameData[0].value"> 
+3

Prawidłowo, ale byłoby pomocne wskazanie w odpowiedzi, że nie można powiązać 'ng-modelu' z funkcją. – HackedByChinese

+0

@HackedByChinese dzięki, dodam twoją notatkę do odpowiedzi – karaxuna

+0

@karaxuna bril dzięki – Pindakaas

28

Używanie dla mnie wartości ng zamiast modelu ng.

+1

Dzięki @Bloodhound to działało dla mnie. –

+0

ng-init może pomóc, jeśli otrzymasz ten błąd w innych sytuacjach. –

5

You can bind ng-model to function

Binding to a getter/setter
Sometimes it's helpful to bind ngModel to a getter/setter function. A getter/setter is a function that returns a representation of the model when called with zero arguments, and sets the internal state of a model when called with an argument. It's sometimes useful to use this for models that have an internal representation that's different from what the model exposes to the view.

index.html

<div ng-controller="ExampleController"> 
    <form name="userForm"> 
    <label>Name: 
     <input type="text" name="userName" 
      ng-model="user.name" 
      ng-model-options="{ getterSetter: true }" /> 
    </label> 
    </form> 
    <pre>user.name = <span ng-bind="user.name()"></span></pre> 
</div> 

app.js

angular.module('getterSetterExample', []) 
.controller('ExampleController', ['$scope', function($scope) { 
    var _name = 'Brian'; 
    $scope.user = { 
    name: function(newName) { 
    // Note that newName can be undefined for two reasons: 
    // 1. Because it is called as a getter and thus called with no arguments 
    // 2. Because the property should actually be set to undefined. This happens e.g. if the 
    // input is invalid 
    return arguments.length ? (_name = newName) : _name; 
    } 
    }; 
}]); 
+0

to nie działa dla mnie – miukki

Powiązane problemy