最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS ng-click not working - Stack Overflow

programmeradmin5浏览0评论

I'm using AngularJS 1.4.2 (with Angular Material) and I'm trying to implement a save function. However, ng-click is not firing on my button. Here is my HTML code:

<div ng-controller="ModelEditController" class="col-lg-12 fontArial slide" ng-repeat="mdl in model">
    <md-content layout-padding>
            <form name="modelEditForm" novalidate role="form" unsaved-warning-form>
            <md-input-container flex style="width:100%">
                        <label>Name</label>
                        <input required name="model_name" ng-model="mdl.model_name" maxlength="100">
                </md-input-container>

        </form>
    </md-content>

    <div class="panel-footer footer" ng-controller="TestController">
        <md-button class="md-fab md-primary md-hue-2" aria-label="Save" ng-click="editModel(mdl)">
                    <md-icon md-svg-src="/img/icons/save.svg"></md-icon>
        </md-button>

    </div>
</div>

And here is my javascript code:

var myApp = angular.module('myApp', ['ui.bootstrap', 'ui.router', 'ngCookies', 'ng-sortable', 'vAccordion', 'ngRoute', 'ngAnimate', 'fiestah.money', 'uiSwitch','ngMaterial','ngMessages','unsavedChanges'])
});


myApp.controller('TestController', ['$scope', 'ModelService', function ($scope,ModelService){

        function editModel(xMdl) {
            console.log("Inside edit model");

        }

    }]);

The "Inside edit model" text is not being output to the console. Why would the ng-click not be working?

I'm using AngularJS 1.4.2 (with Angular Material) and I'm trying to implement a save function. However, ng-click is not firing on my button. Here is my HTML code:

<div ng-controller="ModelEditController" class="col-lg-12 fontArial slide" ng-repeat="mdl in model">
    <md-content layout-padding>
            <form name="modelEditForm" novalidate role="form" unsaved-warning-form>
            <md-input-container flex style="width:100%">
                        <label>Name</label>
                        <input required name="model_name" ng-model="mdl.model_name" maxlength="100">
                </md-input-container>

        </form>
    </md-content>

    <div class="panel-footer footer" ng-controller="TestController">
        <md-button class="md-fab md-primary md-hue-2" aria-label="Save" ng-click="editModel(mdl)">
                    <md-icon md-svg-src="/img/icons/save.svg"></md-icon>
        </md-button>

    </div>
</div>

And here is my javascript code:

var myApp = angular.module('myApp', ['ui.bootstrap', 'ui.router', 'ngCookies', 'ng-sortable', 'vAccordion', 'ngRoute', 'ngAnimate', 'fiestah.money', 'uiSwitch','ngMaterial','ngMessages','unsavedChanges'])
});


myApp.controller('TestController', ['$scope', 'ModelService', function ($scope,ModelService){

        function editModel(xMdl) {
            console.log("Inside edit model");

        }

    }]);

The "Inside edit model" text is not being output to the console. Why would the ng-click not be working?

Share Improve this question edited Aug 3, 2015 at 4:21 javawocky 9152 gold badges10 silver badges31 bronze badges asked Aug 3, 2015 at 3:32 CaptainMorganCaptainMorgan 1,2533 gold badges28 silver badges59 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

That's because your editModel function isn't attached to $scope. Try:

$scope.editModel = function(){ ... };
发布评论

评论列表(0)

  1. 暂无评论