How to set variable AngularJS with a variable C # in the view

0

Neither of these forms works for me and try

<input type="text" ng-model="model1" value="@Model.valor2">

Neither

<input type="text" ng-model="model1" ng-value="@Model.valor2">

    
asked by Anthony Robert Cardenas Vilcap 10.04.2018 в 17:50
source

2 answers

0

Try using the directive ngInit to initialize the value of a model from the view in angularjs :

<div ng-init="model1 = '@Model.valor2'">
  <input type="text" ng-model="model1">
</div>

Here is an example:

angular.module("app",[])
.controller("ctrl",function($scope){
  $scope.model = "";
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="app" ng-controller="ctrl">
  <div ng-init="modelo ='hola mundo'">
    <input type="text" ng-model="modelo" />
  </div>
</div>
    
answered by 11.04.2018 / 02:52
source
0

It is assumed that from the angle controller you should invoke invoking an action using $http to take the value that the server code returns in c#

A mvc view does not apply to an angular controller, they are incompatible, if you use angular then you do not use .cshtml , you use only .html which would be the angle view

The best union between c# and angular is using web api

Hands On Lab: Build a Single Application Page (SPA) with ASP.NET Web API and Angular.js

As you will notice in the article, there is a base that arms the main page using a mvc view using cshtml , but then the rest is retrieved using $http invoking the web api

    
answered by 10.04.2018 в 18:01