2

カスタム入力テキストを取得しようとしています。これが私の見解です

<div>
    <span>{{label}}</span>
    <input type="text" class="{{class}}" placeholder="{{placeholder}}" ng-style="width?{width: width + 'px'}:{}" ng-model="model"/>
</div>

これが私のコントローラーです

    angular.module('inputText', [])
        .directive('inputText', function() {
            return {
                restrict: 'E',
                replace: true,
                scope: {
                    width: '@',
                    label: '@',
                    class: '@',
                    placeholder: '@',
                    model: '@'
                },
                controller: 'InputCtrl',
                templateUrl: 'inputText.html'
            };
        })
        .controller('InputCtrl', ['$scope', function($scope) {

        }]);

問題は、次のようなエラー メッセージが常に表示されることです。

Error: [$parse:syntax] Syntax Error: Token 'model' is unexpected, expecting [:] at column 3 of the expression [{{model}}] starting at [model}}].

私の質問は、次のようなものを取得するために、参照によってモデルを渡すにはどうすればよいですか?

<input-text label="When?" class="" placeholder="" icon="awesome-icon" width="150" model="{{when}}"></input-text>
4

1 に答える 1

2

scope: { model: "=" }モデルを(=は双方向バインディングを意味する)として定義し、以下を使用します。

<input ... ng-model="model" />

テンプレートで。

第 1 レベルのプロパティを使用しないように注意してください。つまり、次のようにします。

<input-text ... model="form.when"></input-text>

そして、これではありません

<input-text ... model="when"></input-text>

すべての場合において、{{when}}これは誤りであり、双方向にバインドされません。

于 2013-10-29T10:40:54.853 に答える