1

ng-model을 함수를 사용하여 특정 문자열로 조건 적으로 초기화 할 수 있습니까? 다음에, NG-바인드 작동하지만 NG-모델은하지 않습니다함수를 사용하여 ng-model 문자열 초기화

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> 

명확히 편집 : 나는 두 개의 별도의 데이터가 주어진 입력에 영향을 수 있다는 설정이있다. 어떤 효과가 모델의 다른 값에 조건부입니다. 수십 가지의 다양한 입력 유형이 있으며, 각 스위치마다 작성하는 것을 피하고 싶습니다. 오히려 혼란 스럽습니다.

예 :http://plnkr.co/edit/fKlbN8qcjGjzqjRayviT?p=preview

+0

당신이 ** 설정하는 기대 방법 ** 그'NG-model'를? 컨트롤러에서 초기화하십시오. – Ian

답변

2

이 조건부 모델의 값을 설정하기 위해 깨끗한 쉬운 방법입니다. 컨트롤러에서 다음

<input ng-model="yourModel" /> :

확인을 명확히 질문에 대한

$scope.yourModel = (condition) ? 'hello1' : 'hello2';

편집, 나는 ng-init를 사용하여 뭔가를 내놓았다. 분명히 ng-model 함수를 사용할 수 없습니다. 나는 그것을 시도하지 않아서 작동하지 않는다는 것에 놀랐다.

사용자 지정 지정 문이 필요하지 않았으므로 버렸습니다. 당신이려고하는 경우에,

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

    $scope.getBinding = function(set, index) 
    { 
    return set[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> 

컨트롤러 :

당신의 HTML은 다음과 같이 될 것입니다 리피터를 사용하면 $ index 값을 사용하여 다음과 같이 모델 이름을 만들 수 있습니다.

<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> 

DEMO - http://plnkr.co/edit/70QG3s3ovppkXcUqOmPj?p=preview

잘하면이 솔루션을 생각해 낼 수 있습니다. 당신이보기에이 작업을 수행 할 "필요가있다"어떤 이유에서 가정

+0

- 명확한 편집을하십시오. – AaronF

+0

나는 당신의 질문을 이해하지 못합니다. 단순화 된 jsfiddle 또는 작동하지 않는 예제를 만들 수 있습니까? – Ronnie

+0

그래. 나는 가능한 한 빨리 그것을 편집으로 추가 할 것입니다. 불명확 한 것에 대해 사과드립니다. – AaronF

0

, 당신은 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; 
    }; 
} 

에 모델을 설정할 수 있습니다 http://jsfiddle.net/dPn3A/

그렇지 않은 경우 컨트롤러에서 수행하십시오.

+0

- 설명 편집. – AaronF

0

이 같은 ngInit 사용할 수 있습니다

<body ng-controller="MainCtrl" ng-init="titel=get(true)"> 
    <div ng-bind="titel"></div> 
    <input ng-model="titel" /> 
    </body> 
+0

- 설명 편집. – AaronF