简体   繁体   中英

angularjs: simple example - ng-class or ng-controller does not work

pls, help me with next question

I try to implement conditional classes on div blocks using ng-class but have encountered next problem - "it is not working". Example seems to be very simple, I even do not know how else it can be described.

But one more idea - controller does not working instead of ng-class.....i do not know.

Here you can see my code pls, probably you will find smth wrong or give me an advice.

http://plnkr.co/edit/Zm0g4QfkqzTD3h4FWfcp?p=preview

demoApp = angular.module('demoApp',[]);

var controllers = {};


controllers.testCntr = function ($scope)
  {
  $scope.setClass = function()
    {
      alert('works');
      return True;
    }
  };

HTML

<!doctype html>
<html ng-app="demoApp">
<head>
    <title></title>

<style>

.red
  {
    color:red;
  }

</style>



    <script src="angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular-route.js"></script>
</head>

<body>

    <div ng-controller="testCntr">
      <span ng-class="{red: $scope.setClass()}">Test color</span>

      <div>{{$scope.setClass()}}</div>

    </div>


</body>


</html> 

Thanks for your amendments.

Several problems here.

  1. You can only add one controller at a time with .controller()
  2. In your template, $scope is already the context, so don't include it in the HTML
  3. True should be true
  4. You are missing an ng-app directive
  5. angular.js is not included properly
  6. Your scripts.js is not included

Here's a working version of your Plunkr with the changes above: http://plnkr.co/edit/ybDScjDrrIkH8tBNfIg1?p=preview

To add the controller to your app you should do this

  demoApp.controller('testCntr', function ($scope) {
    $scope.setClass = function() {
      alert('works');
      return True;
    }
  })

Working Plunkr: http://plnkr.co/edit/LPY94jPJdIJX4pr9K5FY?p=preview

Working Demo:

 demoApp = angular.module('demoApp',[]); demoApp.controller('testCntr', function ($scope) { $scope.setClass = function() { alert('works'); return 33; return True; } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="demoApp" <div="" ng-controller="testCntr"> <span ng-class="{red: $scope.setClass()}">Test color</span> <div>{{setClass()}}</div> </div> 

When you call a function which is in the scope from the view, just call it like that :

  <body ng-app="app">
    <div ng-controller="Ctrl">
      <span ng-class="{'red': setClass()}">Test color</span>
      <div>{{setClass()}}</div>
    </div>
  </body>

Here the Working Plunkr

You should create an app.js services.js and controllers.js

Your controller should be moved to the controllers.js and look a little something like this.

demoApp.controller('testCntr',function ($scope){
  $scope.setClass = (function(){
    alert('works');
    return True;
  })
});

your script.js file should be

var app = angular.module('demoApp',[]);

app.controller('testCntr', ['$scope', function($scope) {

  $scope.setClass = function() {
    return true;
  };

}
]);

and html :

<!DOCTYPE html>
<html ng-app="demoApp">

  <head>
    <script src="angular.min.js"></script>
    <script src="script.js"></script>
    <style>
          .red {
             color:red;
          }
   </style>
  </head>

  <body ng-controller="testCntr">
    <span ng-class="{red: setClass()}">Test color</span>
  </body>

</html>

It may solve your problem

Plunker link: http://plnkr.co/edit/Ftx3M0aG9G8cJtyrn5wj?p=preview

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM