【问题标题】:Angular using ng-model with expression in directive templateAngular 在指令模板中使用带有表达式的 ng-model
【发布时间】:2013-03-24 10:15:36
【问题描述】:

我想在自定义元素中使用 ng-model 属性。问题是,我需要使用表达式设置 ng-model:

ng-model="{{anyVariable}}"

问题是,当我将表达式添加到模板中的 ng-model 属性时,我总是会收到错误:

Error: Syntax Error: Token 'x.name' is unexpected, expecting [:] at column 3 of the expression [{{x.name}}] starting at [x.name}}].
    at Error (<anonymous>)
    at throwError (http://localhost:9000/public/javascripts/angular.js:5999:11)
    at consume (http://localhost:9000/public/javascripts/angular.js:6037:7)
    at object (http://localhost:9000/public/javascripts/angular.js:6327:9)
    at primary (http://localhost:9000/public/javascripts/angular.js:6211:17)
    at unary (http://localhost:9000/public/javascripts/angular.js:6198:14)
    at multiplicative (http://localhost:9000/public/javascripts/angular.js:6181:16)
    at additive (http://localhost:9000/public/javascripts/angular.js:6172:16)
    at relational (http://localhost:9000/public/javascripts/angular.js:6163:16)
    at equality (http://localhost:9000/public/javascripts/angular.js:6154:16) 

使用的代码:

function addFormFieldDirective(elementName, template) {
    app.directive(elementName, function() {
        return {
            restrict: "E",
            scope: {},
            replace: true,
                              // adds some extra layout
            template: buildFormTemplate(template),
            link: function(scope, elm, attrs) {
                scope.x = attrs;
            }
         };
    });
}
addFormFieldDirective("textfield", '<input id="{{x.id}}" ng-model="{{x.name}}" type="text" name="{{x.name}}" value="{{x.value}}" />');

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    试试这个版本:

    .directive('myDir', function() {
        return {
            restrict: 'EA',
            scope:    {
                        YYY: '=ngModel'
                      },
            require:  'ngModel',
            replace:  true,
            template: '<input ng-model="YYY" />'
        };
    });
    

    【讨论】:

    • Angular 1.0.8 不接受函数作为模板:Template must have exactly one root element. was: function render(element, attrs) {return '&lt;input ng-model="YYY" /&gt;';}
    • 正是我需要的。谢谢@malix
    【解决方案2】:

    我找不到在指令模板中将表达式传递给 ng-model 的方法。

    以下解决方案在指令中创建隔离模型,指令controller 在主控制器模型对象中动态创建属性名称并监视隔离模型以将更新传递给主模型:

    app.directive("textfield", function() {
        return {
            restrict: "E",
            scope: {},
            replace: true,
            controller:function($scope,$attrs)  {
                $scope.x=$attrs;
    
                $scope.$watch('directiveModel',function(){
                    $scope.$parent.myModel[$attrs.name]=$scope.directiveModel;
                }) ;
    
                $scope.directiveModel=$attrs.value;
            },
            template: buildFormTemplate('<input ng-model="directiveModel" id="{{x.id}}" type="text" name="{{x.name}}" value="{{x.value}}" />');
    
        };
    });
    

    Plunkr Demo

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    • 2015-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多