Angularjs:来自控制器的ng-model设定值

时间:2014-06-04 15:22:32

标签: angularjs

如何使用控制器中的点设置ng-model的值?

<input type=text" ng-model="user.latitude">

这不起作用:

$scope.user.latitude = myLat;

3 个答案:

答案 0 :(得分:4)

您需要先创建user对象:

$scope.user = {};
$scope.user.latitude = myLat;

或更短:

$scope.user = {latitude: myLat};

答案 1 :(得分:0)

我将users_review作为对象数组,只是为了替换数组元素的特定键,我将代码修改为

using Xamarin;
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Support.V7.App;

namespace Calculator
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme",
MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
    public TextView result;


    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource

        SetContentView(Resource.Layout.activity_main);

        result = FindViewById<TextView>(Resource.Id.result);

    }

    [Java.Interop.Export("ZeroClicked")]
    public void ZeroClicked(object sender, System.EventArgs e)
    {

        result.Text = result.Text + "0";

    }
    [Java.Interop.Export("OneClicked")]
    public void OneClicked(object sender, System.EventArgs e)
    {

        result.Text = result.Text + "1";

    }
    [Java.Interop.Export("TwoClicked")]
    public void TwoClicked(object sender, System.EventArgs e)
    {

        result.Text = result.Text + "2";

    }
    [Java.Interop.Export("ThreeClicked")]
    public void ThreeClicked(object sender, System.EventArgs e)
    {
        result.Text = result.Text + "3";

    }
    [Java.Interop.Export("FourClicked")]
    public void FourClicked(object sender, System.EventArgs e)
    {
        result.Text = result.Text + "4";
    }
    [Java.Interop.Export("FiveClicked")]
    public void FiveClicked(object sender, System.EventArgs e)
    {
        result.Text = result.Text + "5";
    }
    [Java.Interop.Export("SixClicked")]
    public void SixClicked(object sender, System.EventArgs e)
    {
        result.Text = result.Text + "6";
    }
    [Java.Interop.Export("SevenClicked")]
    public void SevenClicked(object sender, System.EventArgs e)
    {
        result.Text = result.Text + "7";
    }
    [Java.Interop.Export("EightClicked")]
    public void EightClicked(object sender, System.EventArgs e)
    {
        result.Text = result.Text + "8";
    }
    [Java.Interop.Export("NineClicked")]
    public void NineClicked(object sender, System.EventArgs e)
    {
        result.Text = result.Text + "9";
    }
    [Java.Interop.Export("PlusClicked")]
    public void PlusClicked(object sender, System.EventArgs e)
    {
        result.Text = result.Text + "+";
    }
    [Java.Interop.Export("MinusClicked")]
    public void MinusClicked(object sender, System.EventArgs e)
    {
        result.Text = result.Text + "-";
    }
    [Java.Interop.Export("MultiplicationClicked")]
    public void MultiplicationClicked(object sender, System.EventArgs e)
    {
        result.Text = result.Text + "*";
    }
    [Java.Interop.Export("DivisionClicked")]
    public void DivisionClicked(object sender, System.EventArgs e)
    {
        result.Text = result.Text + "/";
    }
    [Java.Interop.Export("LeftBracketClicked")]
    public void LeftBracketClicked(object sender, System.EventArgs e)
    {
        result.Text = result.Text + "(";
    }


    [Java.Interop.Export("RightBracketClicked")]
    public void RightBracketClicked(object sender, System.EventArgs e)
    {
        result.Text = result.Text + ")";
    }
}

答案 2 :(得分:0)

这是唯一可行的方法。您必须先创建一个对象,然后才能在其中输入内容。

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

First Name: <input type="text" ng-model="name.fname"><br>



</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
	$scope.name={};
   $scope.name.fname="test";
   
   //$scope.name={fname:"test"};
});
</script>

</body>
</html>