Is there any way to interact with the controller and scope directly in Angularjs?

I defined a controller, and there are some variables in the area. Can I find out if there is a way to assign a value to a scope variable directly outside the controller (and not ng-model)? Also, can I call the controller function directly somewhere in the <script>webpage element (instead of using ng-click)?

Thank!

Cheers, Chris

+5
source share
1 answer

angular , . HTML, , angular . , .

, , . html, -

var scope = angular.element("#myelement").scope();
//use the scope....

, , .

UPDATE

bootstrap...

<tab>
<pane title="tab1"><pane>
<pane title="tab2"></pane>
</tabs>

, http://angularjs.org/. .

var directives = angular.module('myApp.directives', []);
directives.directive('tabs', function () {
    return {
        restrict:'E',
        transclude:true,
        scope:{},
        controller:function ($scope, $element, $rootScope) {
            var panes = $scope.panes = [];

            $scope.select = function (pane) {
                angular.forEach(panes, function (pane) {
                    pane.selected = false;
                });
                pane.selected = true;
                $rootScope.$broadcast("tabChanged", pane.title);
            }

            this.addPane = function (pane) {
                if (panes.length == 0) $scope.select(pane);
                panes.push(pane);
            }
        },
        template:'<div class="tabbable">' +
            '<ul class="nav nav-tabs">' +
            '<li ng-repeat="pane in panes" ng-class="{active:pane.selected}">' +
            '<a href="" ng-click="select(pane)">{{pane.title}}</a>' +
            '</li>' +
            '</ul>' +
            '<div class="tab-content" ng-transclude></div>' +
            '</div>',
        replace:true
    };
});
directives.directive('pane', function () {
    return {
        require:'^tabs',
        restrict:'E',
        transclude:true,
        scope:{ title:'bind' },
        link:function (scope, element, attrs, tabsCtrl) {
            tabsCtrl.addPane(scope);
        },
        template:'<div class="tab-pane" ng-class="{active: selected}" ng-transclude>' +
            '</div>',
        replace:true
    };
});

.

+11

All Articles