AngularJS Material

John241
Tera Expert

Hi ! 

I tried to use AngularJS Material in a widget. 

For example I tried to reproduce the 'Basic Usage' input in a widget : https://material.angularjs.org/latest/demo/input

So I created a widget with the code below, according to this page : https://material.angularjs.org/1.1.22-rc.0/getting-started

I tried to add ngMaterial as dependency according to this article: https://community.servicenow.com/community?id=community_question&sys_id=a0e100b1dbda27407d3e02d5ca96...

but when I check 'Include on page load' I get a blank page. 

Can you help me please ? 

I have another question : is angular material (not JS) is working on servicenow or we should use only angularJS material? 

Kinds regards, 

find_real_file.png

 

<html lang="en" >
<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!-- Angular Material style sheet -->
  <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.12/angular-material.min.css">
</head>
<body ng-app="BlankApp" ng-cloak>
<div ng-controller="DemoCtrl" layout="column" ng-cloak="" class="md-inline-form inputdemoBasicUsage" ng-app="MyApp">

  <md-content md-theme="docs-dark" layout-gt-sm="row" layout-padding="">
    <div>
      <md-input-container>
        <label>Title</label>
        <input ng-model="user.title">
      </md-input-container>

      <md-input-container>
        <label>Email</label>
        <input ng-model="user.email" type="email">
      </md-input-container>
    </div>
  </md-content>

  <md-content layout-padding="">
    <div>
      <form name="userForm">

        <div layout-gt-xs="row">
          <md-input-container class="md-block" flex-gt-xs="">
            <label>Company (Disabled)</label>
            <input ng-model="user.company" disabled="">
          </md-input-container>

          <md-input-container>
            <label>Enter date</label>
            <md-datepicker ng-model="user.submissionDate" aria-label="Enter date"></md-datepicker>
          </md-input-container>
        </div>

        <div layout-gt-sm="row">
          <md-input-container class="md-block" flex-gt-sm="">
            <label>First name</label>
            <input ng-model="user.firstName">
          </md-input-container>

          <md-input-container class="md-block" flex-gt-sm="">
            <label>Long Last Name That Will Be Truncated And 3 Dots (Ellipsis) Will Appear At The End</label>
            <input ng-model="theMax">
          </md-input-container>
        </div>

        <md-input-container class="md-block">
          <label>Address</label>
          <input ng-model="user.address">
        </md-input-container>

        <md-input-container md-no-float="" class="md-block">
          <input ng-model="user.address2" placeholder="Address 2" aria-label="Address 2">
        </md-input-container>

        <div layout-gt-sm="row">
          <md-input-container class="md-block" flex-gt-sm="">
            <label>City</label>
            <input ng-model="user.city">
          </md-input-container>

          <md-input-container class="md-block" flex-gt-sm="">
            <label>State</label>
            <md-select ng-model="user.state">
              <md-option ng-repeat="state in states" value="{{state.abbrev}}">
                {{state.abbrev}}
              </md-option>
            </md-select>
          </md-input-container>

          <md-input-container class="md-block" flex-gt-sm="">
            <label>Postal Code</label>
            <input name="postalCode" ng-model="user.postalCode" placeholder="12345" required="" ng-pattern="/^[0-9]{5}$/" md-maxlength="5">

            <div ng-messages="userForm.postalCode.$error" role="alert" multiple="">
              <div ng-message="required" class="my-message">You must supply a postal code.</div>
              <div ng-message="pattern" class="my-message">That doesn't look like a valid postal
                code.
              </div>
              <div ng-message="md-maxlength" class="my-message">
                Don't use the long version silly...we don't need to be that specific...
              </div>
            </div>
          </md-input-container>
        </div>

        <md-input-container class="md-block">
          <label>Biography</label>
          <textarea ng-model="user.biography" md-maxlength="150" rows="5" md-select-on-focus=""></textarea>
        </md-input-container>


      </form>
    </div>
  </md-content>

</div>

<!--
Copyright 2018 Google LLC. All Rights Reserved. 
Use of this source code is governed by an MIT-style license that can be found
in the LICENSE file at http://material.angularjs.org/HEAD/license.
-->

<!--
Copyright 2018 Google LLC. All Rights Reserved. 
Use of this source code is governed by an MIT-style license that can be found
in the LICENSE file at http://material.angularjs.org/HEAD/license.
-->
  
  <!-- Angular Material requires Angular.js Libraries -->
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-animate.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-aria.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular-messages.min.js"></script>

  <!-- Angular Material Library -->
  <script src="https://ajax.googleapis.com/ajax/libs/angular_material/1.1.12/angular-material.min.js"></script>
  
  <!-- Your application bootstrap  -->
  <script type="text/javascript">    
    /**
     * You must include the dependency on 'ngMaterial' 
     */
    angular.module('BlankApp', ['ngMaterial', 'ngMessages']);
    
    angular
  .module('MyApp', ['ngMaterial', 'ngMessages', 'material.svgAssetsCache'])
  .controller('DemoCtrl', function($scope) {
    $scope.user = {
      title: 'Developer',
      email: 'ipsum@lorem.com',
      firstName: '',
      lastName: '',
      company: 'Google',
      address: '1600 Amphitheatre Pkwy',
      city: 'Mountain View',
      state: 'CA',
      biography: 'Loves kittens, snowboarding, and can type at 130 WPM.\n\nAnd rumor has it she bouldered up Castle Craig!',
      postalCode: '94043'
    };

    $scope.states = ('AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD MA MI MN MS ' +
    'MO MT NE NV NH NJ NM NY NC ND OH OK OR PA RI SC SD TN TX UT VT VA WA WV WI ' +
    'WY').split(' ').map(function(state) {
        return {abbrev: state};
      });
  })
  .config(function($mdThemingProvider) {

    // Configure a dark theme with primary foreground yellow

    $mdThemingProvider.theme('docs-dark', 'default')
      .primaryPalette('yellow')
      .dark();

  });


/**
Copyright 2018 Google LLC. All Rights Reserved. 
Use of this source code is governed by an MIT-style license that can be found
in the LICENSE file at http://material.angularjs.org/HEAD/license.
**/


/**
Copyright 2018 Google LLC. All Rights Reserved. 
Use of this source code is governed by an MIT-style license that can be found
in the LICENSE file at http://material.angularjs.org/HEAD/license.
**/
  </script>

</body>
</html>

<!--
Copyright 2016-2019 Google Inc. All Rights Reserved. 
Use of this source code is governed by an MIT-style license that can be found in the LICENSE file at https://material.angularjs.org/latest/license.
-->

 

1 ACCEPTED SOLUTION

tomashrobarik
Giga Guru

It seems that your dependency contains the core angularjs library. This is not necessary as it is provided by service portal. What you actually need inside that dependency are following files:

following JS include

https://ajax.googleapis.com/ajax/libs/angular_material/1.1.20/angular-material.min.js

and following stylesheet:

https://ajax.googleapis.com/ajax/libs/angular_material/1.1.20/angular-material.min.css

Secondly, if you are putting the whole html code mentioned above into the widget, I highly recommend to firstly read something about service portal and maybe doing some tutorials. The widgets are just "inner" parts of the web page, so no need to put whole html page structure there. Try to put there something real simple like

<md-button class="md-raised md-primary">Primary</md-button>

Lastly, the https://material.angular.io/requires the latest angular version which is not part of the service portal architecture. The service portal is running the first version of AngularJS so you can only use the https://material.angularjs.org

View solution in original post

12 REPLIES 12

tomashrobarik
Giga Guru

It seems that your dependency contains the core angularjs library. This is not necessary as it is provided by service portal. What you actually need inside that dependency are following files:

following JS include

https://ajax.googleapis.com/ajax/libs/angular_material/1.1.20/angular-material.min.js

and following stylesheet:

https://ajax.googleapis.com/ajax/libs/angular_material/1.1.20/angular-material.min.css

Secondly, if you are putting the whole html code mentioned above into the widget, I highly recommend to firstly read something about service portal and maybe doing some tutorials. The widgets are just "inner" parts of the web page, so no need to put whole html page structure there. Try to put there something real simple like

<md-button class="md-raised md-primary">Primary</md-button>

Lastly, the https://material.angular.io/requires the latest angular version which is not part of the service portal architecture. The service portal is running the first version of AngularJS so you can only use the https://material.angularjs.org

Hello, 

 

Thanks for your anwser, I have added the following JS  :

https://ajax.googleapis.com/ajax/libs/angularjs/1.7.6/angular.min.js
https://ajax.googleapis.com/ajax/libs/angular_material/1.1.12/angular-material.min.js

and this CSS :

https://ajax.googleapis.com/ajax/libs/angular_material/1.1.12/angular-material.min.css

Now it's working.

Kinds regards,
 

As you said AngularJS is not necessary so I delete it and replace angular-material 1.1.12 by the link you provided. 

 

I have another problem, maybe you can help me, I attach screenshots : 

 

find_real_file.png

 

 

 

find_real_file.png

 

<md-input-container class="md-block" flex-gt-sm="">
            <label translate>Postal Code</label>
            <input name="postalCode" ng-model="c.user.postalCode" placeholder="12345" required="" ng-pattern="/^[0-9]{5}$/" md-maxlength="5">

            <pre>userForm.postalCode.$error = {{ userForm.postalCode.$error | json }}</pre>
            
          <div ng-messages="userForm.postalCode.$error" role="alert" multiple="">
              <div ng-message="required" class="my-message">You must supply a postal code.</div>
              <div ng-message="pattern" class="my-message">That doesn't look like a valid postal
                code.
              </div>
              <div ng-message="md-maxlength" class="my-message">
                Don't use the long version silly...we don't need to be that specific...
              </div>
            </div>
          </md-input-container>

 

Kinds regards, 

Hi, it seems that you are trying the sample demo for input. It helped me to define a new dependency with angular module name ngMessages with following script.

https://ajax.googleapis.com/ajax/libs/angularjs/1.6.1/angular-messages.min.js

Then I attached that dependendency to the widget. It is not needed to check the include on page load.