Javascript 使用函数初始化ng模型字符串

Javascript 使用函数初始化ng模型字符串,javascript,angularjs,angularjs-directive,angularjs-scope,angular-ngmodel,Javascript,Angularjs,Angularjs Directive,Angularjs Scope,Angular Ngmodel,有没有一种方法可以通过函数有条件地将ng模型初始化为某个字符串?在以下情况中,ng bind起作用,但ng模型不起作用: var app = angular.module('app', []); app.controller('mainCtrl', ['$scope', function ($scope) { $scope.value = "hello"; $scope.value2 = "hello2"; $scope.get = function (bool) { r

有没有一种方法可以通过函数有条件地将ng模型初始化为某个字符串?在以下情况中,ng bind起作用,但ng模型不起作用:

var app = angular.module('app', []);
app.controller('mainCtrl', ['$scope', function ($scope) {
  $scope.value = "hello";
  $scope.value2 = "hello2";
  $scope.get = function (bool) {
      return (bool) ? "value" : "value2";
  };
}]);

<html ng-app="app">
  <body ng-controller="mainCtrl">
    <div ng-bind="{{get(true)}}"></div>
    <input ng-model="{{get(true)}}" />
  </body>
</html>
var-app=angular.module('app',[]);
app.controller('mainCtrl',['$scope',函数($scope){
$scope.value=“你好”;
$scope.value2=“hello2”;
$scope.get=函数(bool){
返回值(bool)-“值”:“值2”;
};
}]);
澄清编辑: 我有两个独立的数据集,一个给定的输入可以影响。它影响哪一个取决于模型中其他地方的值。有几十种不同的输入类型,我希望避免在每一种输入类型中都写入ng开关,因为这样会变得相当混乱


示例:

这是有条件地设置模型值的最干净、最简单的方法

然后在控制器中:

app.controller('mainCtrl', ['$scope', function ($scope) {
  $scope.setA = ['a', 'aa', 'aaa'];
  $scope.setB = ['b', 'bb', 'bbb'];

  $scope.getBinding = function(set, index)
  {
    return set[index];
  }
}]);
$scope.yourModel=(条件)?'hello1’:‘hello2’

编辑澄清问题

好的,我用
nginit
想出了一些东西。显然,您不能将函数与
ng model
一起使用。我还没有试过,所以我很惊讶它没有起作用

我放弃了定制指令,因为它不需要

您的HTML将如下所示:

<div ng-init="input1 = getBinding(setB,2)">
  <input ng-model="input1" /> <!-- shows bbb -->
</div>
<div ng-init="input2 = getBinding(setA,0)">
  <input ng-model="input2" /> <!-- shows a -->
</div>
<div ng-init="input3 = getBinding(setA,2)">
  <input ng-model="input3" /> <!-- shows aaa -->
</div>
<div ng-repeat="setItem in setA" ng-init="setA$index = getBinding(setA, $index)">
  <input ng-model="setA$index" /> <!-- shows 3 inputs of values a, aa, aaa -->
</div>

<div ng-repeat="setItem in setB" ng-init="setB$index = getBinding(setB, $index)">
  <input ng-model="setB$index" /> <!-- shows 3 inputs of values b, bb, bbb-->
</div>
  <body ng-controller="MainCtrl" ng-init="titel=get(true)">
    <div ng-bind="titel"></div>
    <input ng-model="titel" />
  </body>
因此,向前看,如果要使用中继器,可以使用$index值创建模型名称,而不是像
input1=…
input2=…
等那样设置名称:

<div ng-init="input1 = getBinding(setB,2)">
  <input ng-model="input1" /> <!-- shows bbb -->
</div>
<div ng-init="input2 = getBinding(setA,0)">
  <input ng-model="input2" /> <!-- shows a -->
</div>
<div ng-init="input3 = getBinding(setA,2)">
  <input ng-model="input3" /> <!-- shows aaa -->
</div>
<div ng-repeat="setItem in setA" ng-init="setA$index = getBinding(setA, $index)">
  <input ng-model="setA$index" /> <!-- shows 3 inputs of values a, aa, aaa -->
</div>

<div ng-repeat="setItem in setB" ng-init="setB$index = getBinding(setB, $index)">
  <input ng-model="setB$index" /> <!-- shows 3 inputs of values b, bb, bbb-->
</div>
  <body ng-controller="MainCtrl" ng-init="titel=get(true)">
    <div ng-bind="titel"></div>
    <input ng-model="titel" />
  </body>

演示-


希望您能找到解决方案。

假设出于任何原因您需要在视图中执行此操作,您可以使用init将变量设置为get函数的返回值,并将模型设置为该值

<div ng-controller="MyCtrl">
    <input ng-model="whatever.value" ng-init="whatever = get(true)" />
    {{var1}}
    {{whatever}}
</div>

function MyCtrl($scope) {
    $scope.var1 = { value: "hello" };
    $scope.var2 = { value: "hello2" };
    $scope.get = function (bool) {
        return (bool) ? $scope.var1 : $scope.var2;
    };
}

{{var1}}
{{随便}}
函数MyCtrl($scope){
$scope.var1={value:“hello”};
$scope.var2={value:“hello2”};
$scope.get=函数(bool){
返回(bool)?$scope.var1:$scope.var2;
};
}


否则,只需在控制器中执行此操作。

您可以像这样使用ngInit:

<div ng-init="input1 = getBinding(setB,2)">
  <input ng-model="input1" /> <!-- shows bbb -->
</div>
<div ng-init="input2 = getBinding(setA,0)">
  <input ng-model="input2" /> <!-- shows a -->
</div>
<div ng-init="input3 = getBinding(setA,2)">
  <input ng-model="input3" /> <!-- shows aaa -->
</div>
<div ng-repeat="setItem in setA" ng-init="setA$index = getBinding(setA, $index)">
  <input ng-model="setA$index" /> <!-- shows 3 inputs of values a, aa, aaa -->
</div>

<div ng-repeat="setItem in setB" ng-init="setB$index = getBinding(setB, $index)">
  <input ng-model="setB$index" /> <!-- shows 3 inputs of values b, bb, bbb-->
</div>
  <body ng-controller="MainCtrl" ng-init="titel=get(true)">
    <div ng-bind="titel"></div>
    <input ng-model="titel" />
  </body>


您希望如何设置ng车型
?在控制器中初始化我不理解你的问题。你能不能创建一个简化的JSFIDLE或者这个例子不起作用?当然可以。我会尽快完成,并将其添加为编辑。我为不清楚而道歉。我给你加了一个plunker让你看。太棒了。绝对是所有答案中最清楚的。