0

我已经使用 AngularJS 几天了,我声明了两个指令,如果我不需要任何东西,它们就可以工作。但我需要ngModel使用该$setViewValue方法。

我的指令如下所示:

<corina>
    <properties data-ng-repeat="property in Properties"></properties>
</corina>

这些指令的定义如下:

var Application = Application || {};

;(function(window, document, undefined) {

    "use strict";

    var CorinaJSDialog = {};

    Application.CorinaJSDialog = angular.module("CorinaJSDialog", ["ngSanitize"]);

    Application.CorinaJSDialog.constant("CorinaJSAPI", {

        document : {
            load : "/corina/api/loaddocument",
            save : "/corina/api/savedocument"
        }
    });    

    Application.CorinaJSDialog.directive("corina", ["$timeout", function($timeout){

        var object = {

            restrict : "E",
            require : "ngModel",

            transclude : true,
            replace : true,
            priority : 1,
            templateUrl : "corina.html",
            controller : function($scope) {},
            link : function(scope, element, attrs, controller) { console.log(controller); }
        };

        return object;
    }]);

    Application.CorinaJSDialog.directive("properties", ["$timeout", function($timeout){

        var object = {

            restrict : "E",
            require : "?corina",
            transclude : true,
            replace : true,
            priority : 2,
            terminal : true,
            templateUrl : "properties.html",
            controller : function($scope) {},
            link : function(scope, element, attrs, controller) {

                var loadXeditable = function() {

                    $(element).editable({
                        mode: "inline",
                        display: function(value) {

                            controller.$setViewValue(value);

                            scope.$apply();

                            $(this).html(value);
                        }
                    });
                };

                $timeout(function() {
                    loadXeditable();
                }, 10);

            }
        };

        return object;
    }]);

})(window, document);

以及加载的模板:

corina.html

<div>
    <div data-ng-transclude></div>
</div>

properties.html

<div>
    <div class="row">
        <span class="span4">{{property.Name}}</span>

        <div class="span8">
            <a href="#" data-type="{{property.Type | lowercase}}" data-ng-model="property.Value" data-name="{{ property.Name }}" data-placeholder="{{ property.Value }}">
                <span data-ng-bind-html="{{ property.Value.toString() }}"></span>
            </a>
        </div>
    </div>
</div>

每次我运行上面我得到这个错误:

Error: No controller: ngModel
at Error (<anonymous>)
at i (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:41:165)
at l (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:43:252)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:47:425
at https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:96:330
at h (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:78:207)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:78:440
at Object.e.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:89:272)
at Object.e.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:87:124)
at Object.e.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.1.3/angular.min.js:89:431) <div data-ng-transclude=""> angular.min.js:62

此外,如果我需要类似 的指令^mydir,我会得到相同的错误,或者只是类似mydir. 只有当我使用?mydir我没有得到错误,但我不能使用ngModule指令。我会很感激一些关于我做错了什么的意见。

4

1 回答 1

3

正如我所解释的那样,您有两个元素指令,corina并且properties,properties需要控制器来自corina. 那总是会失败,因为它们需要在同一个元素上require: 'corina'才能工作。但是我现在在你的模板中看到propertiescorina,所以你应该做的properties是:require : "^?corina"代替。这也告诉 Angular 在父元素中寻找 corina 控制器。

但是要求ngModelincorina不起作用,因为ng-model没有在 corina 元素上设置。你ng-model是在替换模板内的一个元素上设置的corina,因此corina指令无法获取该 ngModelController。您只能从同一元素或父元素请求控制器,而不能从子元素请求控制器。如果您真的需要,您应该在具有和需要ngModelController的相同元素上添加自己的指令。该指令可以要求控制器(因为控制器位于父元素上)并将 传递给控制器​​。ng-modelngModelControllercorinacorinangModelControllercorina

但我不太确定你为什么首先要在ng-model这里使用?您已将其放置在不会做任何事情的锚元素上。你ng-model对锚元素的影响是什么?

于 2013-02-27T06:15:32.880 回答